I am trying to use pickr
package ( a color picker library) in my nuxt.js
app , at import time it is providing error called window is undefined
here is code:
JavaScript
x
21
21
1
<script>
2
import Pickr from '@simonwep/pickr/dist/pickr.min.js';
3
4
let pickr;
5
6
export default {
7
name: "ColorPicker",
8
mounted(){
9
pickr = Pickr.create({
10
el: '.test-picker',
11
theme: 'classic',
12
swatches: [
13
'rgba(244, 67, 54, 1)',
14
'rgba(233, 30, 99, 0.95)',
15
],
16
17
});
18
}
19
}
20
</script>
21
tried approaches
- i made
pickr
package as a nuxt plugin added it tonuxt.config.js
withmode:client
- i made
pickr
package as a nuxt plugin added it tonuxt.config.js
withssr:false
but it did not work 😥
Advertisement
Answer
After struggles i came up with this approach and did work for me
JavaScript
1
27
27
1
<script>
2
3
let PickrInstance;
4
5
if(process.browser){
6
PickrInstance = require('@simonwep/pickr/dist/pickr.min.js')
7
}
8
9
let pickr;
10
11
export default {
12
name: "ColorPicker",
13
mounted(){
14
pickr = PickrInstance.create({
15
el: '.test-picker',
16
theme: 'classic',
17
swatches: [
18
'rgba(244, 67, 54, 1)',
19
'rgba(233, 30, 99, 0.95)',
20
],
21
22
});
23
}
24
}
25
</script>
26
27