I’m trying to use the ButtonCounter component as an example (https://vuejs.org/guide/essentials/component-basics.html#defining-a-component), but I just can’t make it work. I use vue js 3 from a CDN.
I have the ButtonCounter.js file:
export default {
data() {
return {
count: 0
}
},
template: `
<button @click="count++">
You clicked me {{ count }} times.
</button>`
}
Then I have the main js file with vue:
import ButtonCounter from './ButtonCounter.js'
const app = Vue.createApp({
components: {
ButtonCounter
},
data() {
return {
aaa: []
}
},
methods: {
...
}
}
})
app.mount('#app')
And finally I have the html where I link vue js from a cdn, and I specify the following inside the body:
<ButtonCounter />
But I can’t see the button. What am I doing wrong?
2
Answers
Eventually I managed to solve it by using the vue-loader project, see https://github.com/FranckFreiburger/vue3-sfc-loader. It's a great tool!
index.html:
main.js:
ButtonCounter.vue:
What you put inside the
<div id=app></div>
in the index.html is over-written when you have mounted your vue application.It should work if you put it in the template of your main.js file:
Note that if you have not said
type=module
on the script tag for your main.js then it will blow up when you try to import