2👍
✅
On of these may be due to the way you pasted in the question, but your code had some problems:
- The
Vue.component
didn’t declare its tag name.- Notice I added
comments-component
inVue.component('comments-component', {
.
- Notice I added
- The
template
of the component (template:`<div>{{message}}</div>`
) declared amessage
variable that was not present.- I added
message: "check the console",
todata()
.
- I added
At this point, the mounted
and this.comments
in
mounted: function() {
console.log(this.comments)
}
Work as expected.
See demo below.
Vue.component('comments-component', {
template:`<div>{{message}}</div>`,
data() {
return {
message: "check the console",
comments: [
{title: 'hello' , message: 'world'},
{title: 'hellowww' , message: 'worldssss'},
]}
},
mounted: function() {
console.log(this.comments)
}
});
var app = new Vue({
el: '#root'
});
<script src="https://unpkg.com/vue@2.5.13/dist/vue.min.js"></script>
<div id="root">
<comments-component></comments-component>
</div>
Source:stackexchange.com