Vue.js unknown custom element

前端 未结 9 2005
我在风中等你
我在风中等你 2020-12-02 11:01

I\'m a beginner with Vue.js and I\'m trying to create an app that caters my daily tasks and I ran into Vue Components. So below is what I\'ve tried but unfortunately, it giv

相关标签:
9条回答
  • 2020-12-02 11:20

    You forgot about the components section in your Vue initialization. So Vue actually doesn't know about your component.

    Change it to:

    var myTask = Vue.component('my-task', {
     template: '#task-template',
     data: function() {
      return this.tasks; //Notice: in components data should return an object. For example "return { someProp: 1 }"
     },
     props: ['task']
    });
    
    new Vue({
     el: '#app',
     data: {
      tasks: [{
        name: "task 1",
        completed: false
       },
       {
        name: "task 2",
        completed: false
       },
       {
        name: "task 3",
        completed: true
       }
      ]
     },
     components: {
      myTask: myTask
     },
     methods: {
    
     },
     computed: {
    
     },
     ready: function() {
    
     }
    
    });
    

    And here is jsBin, where all seems to works correctly: http://jsbin.com/lahawepube/edit?html,js,output

    UPDATE

    Sometimes you want your components to be globally visible to other components.

    In this case you need to register your components in this way, before your Vue initialization or export (in case if you want to register component from the other component)

    Vue.component('exampleComponent', require('./components/ExampleComponent.vue')); //component name should be in camel-case
    

    After you can add your component inside your vue el:

    <example-component></example-component>
    
    0 讨论(0)
  • 2020-12-02 11:21

    Be sure that you have added the component to the components.

    For example:

    export default {
    data() {
        return {}
    },
    components: {
        'lead-status-modal': LeadStatusModal,
    },
    }
    
    0 讨论(0)
  • 2020-12-02 11:22

    I was following along the Vue documentation at https://vuejs.org/v2/guide/index.html when I ran into this issue.

    Later they clarify the syntax:

    So far, we’ve only registered components globally, using Vue.component:

       Vue.component('my-component-name', {
           // ... options ...
       })
    

    Globally registered components can be used in the template of any root Vue instance (new Vue) created afterwards – and even inside all >subcomponents of that Vue instance’s component tree.

    (https://vuejs.org/v2/guide/components.html#Organizing-Components)

    So as Umesh Kadam says above, just make sure the global component definition comes before the var app = new Vue({}) instantiation.

    0 讨论(0)
  • 2020-12-02 11:25

    This solved it for me: I supplied a third argument being an object.

    in app.js (working with laravel and webpack):

    Vue.component('news-item', require('./components/NewsItem.vue'), {
        name: 'news-item'
    });
    
    0 讨论(0)
  • 2020-12-02 11:33

    Just define Vue.component() before new vue().

    Vue.component('my-task',{
         .......
    });
    
    new Vue({
        .......
    });
    

    update

    • HTML converts <anyTag> to <anytag>
    • So don't use captital letters for component's name
    • Instead of <myTag> use <my-tag>

    Github issue : https://github.com/vuejs/vue/issues/2308

    0 讨论(0)
  • 2020-12-02 11:37

    This is a good way to create a component in vue.

    let template = `<ul>
      <li>Your data here</li>
    </ul>`;
    
    Vue.component('my-task', {
      template: template,
      data() {
    
      },
      props: {
        task: {
          type: String
        }
      },
      methods: {
    
      },
      computed: {
    
      },
      ready() {
    
      }
    });
    
    new Vue({
     el : '#app'
    });
    
    0 讨论(0)
提交回复
热议问题