簡體   English   中英

VueJS 組件不加載

[英]VueJS Component does not load

我有以下 vuejs 組件。 我知道這是一個非常簡單的示例,但仍然無法正確加載。 在我的代碼下面找到:

 Vue.component('my-component', { template: '<div>{{ msg }}</div>', data: { msg: 'hello' } }); new Vue({ el: '#app-6' });
 <!DOCTYPE html> <html lang="en"> <meta> <meta charset="UTF-8"> <title>Components in Vue.js</title> </meta> <body> <div id="app-6"> test <my-component></my-component> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.6/vue.min.js"></script> </body> </html>

有什么建議我做錯了嗎?

感謝您的回復!

數據必須始終是一個函數

 var data={msg: 'hello'} Vue.component('my-component', { template: '<div>{{ msg }}</div>', data:function() { return data; } }); new Vue({ el: '#app-6' });
 <!DOCTYPE html> <html lang="en"> <meta> <meta charset="UTF-8"> <title>Components in Vue.js</title> </meta> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.6/vue.min.js"></script> <div id="app-6"> test <my-component></my-component> </div> </body> </html>

您的數據應該是這樣的函數:

data () {
    return {
        msg: 'Hello'
    }
}

更多信息在這里: https ://v2.vuejs.org/v2/guide/components.html#data-Must-Be-a-Function

你應該這樣做。

Vue.component('my-component', {
  template: '<div>{{ msg }}</div>',
   data: function () {
    return {
      msg: 'Hello'
   }
}
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM