简体   繁体   English

Vuejs:Vue.component 似乎不起作用

[英]Vuejs : Vue.component not seems to work

I am unsure am I doing something wrong but the vuejs component I declared is not working.我不确定我做错了什么,但我声明的 vuejs 组件不起作用。

    <script src="https://unpkg.com/vue"></script>

<div id="app">
  <p>{{ message }}</p>

</div>

  <ol>
  <!-- Create an instance of the todo-item component -->
  <todo-item></todo-item>
</ol>

JavaScript: JavaScript:

var a = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  }
})

Vue.component('todo-item', 
{
  template: '<li>This is a todo</li>'
})

I am using the JsFiddle, message part does display the data but the todo-item didn't show anything.我正在使用 JsFiddle,消息部分确实显示了数据,但 todo-item 没有显示任何内容。

Vue.component has to be called before new Vue in order for the Vue instance to use the component. Vue.component必须在new Vue之前调用,以便 Vue 实例使用该组件。 From the docs (which aren't all that clear on this, admittedly): 从文档中(诚然,这并不是很清楚):

These components are globally registered.这些组件是全局注册的。 That means they can be used in the template of any root Vue instance (new Vue) created after registration.这意味着它们可以在注册创建的任何根 Vue 实例(新 Vue)的模板中使用。

Vue.component('todo-item', {
  template: '<li>This is a todo</li>'
})

var a = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  }
})

That, and your app's code has to all be inside of the #app div, as pointed out by CUGreen's answer.正如 CUGreen 的回答所指出的那样,您的应用程序代码必须全部位于#app div 中。

Your component needs to inside your "#app" div.您的组件需要在您的“#app”div 中。

<script src="https://unpkg.com/vue"></script>

<div id="app">
    <p>{{ message }}</p>
    <ol>
        <!-- Create an instance of the todo-item component -->
        <todo-item></todo-item>
    </ol>
</div>

Here is a working fiddle .这是一个工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM