Vue.js未知的自定义元素


89

我是Vue.js的初学者,我试图创建一个能够满足日常任务的应用程序,然后遇到了Vue Components。因此,以下是我尝试过的操作,但不幸的是,它给了我这个错误:

vue.js:1023 [Vue警告]:未知的自定义元素:-您是否正确注册了组件?对于递归组件,请确保提供“名称”选项。

有什么想法,请帮忙吗?

new Vue({
  el : '#app',
  data : {
    tasks : [
      { name : "task 1", completed : false},
      { name : "task 2", completed : false},
      { name : "task 3", completed : true}
    ]
  },
  methods : {
  
  },
  computed : {
  
  },
  ready : function(){

  }

});

Vue.component('my-task',{
  template : '#task-template',
  data : function(){
    return this.tasks
  },
  props : ['task']
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.js"></script>
<div id="app">
  <div v-for="task in tasks">
      <my-task :task="task"></my-task>
  </div>
  
</div>

<template id="task-template">
  <h1>My tasks</h1>
  <div class="">{{ task.name }}</div>
</template>

Answers:


122

您忘记了中的components部分Vue initialization。因此,Vue实际上并不了解您的组件。

更改为:

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() {

 }

});

这是jsBin,似乎一切正常:http ://jsbin.com/lahawepube/edit?html,js,output

更新

有时,您希望组件对其他组件全局可见。

在这种情况下,您需要在Vue initialization或之前export(如果要从其他组件注册组件)以这种方式注册组件。

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

在您可以在以下位置添加组件之后vue el

<example-component></example-component>

因此,如果我有很多组件,那么应该将其绑定到组件阵列上吗?
Juliver Galleto

@CodeDemon肯定。如果要使用ES6,可以缩短它的长度,components: {myTask},或者,如果它是一个很小的组件,则可以在componentsproperty内部创建它。但这似乎不是一个好方法,因为应用程序规模很大。
GONG

我为什么会有这个Vue警告的任何想法:vue.js:1023 [Vue警告]:数据函数应该返回一个对象。(位于组件:<my-task>中)?
Juliver Galleto

4
您需要在组件中这样写: data: function() { return { property1: 1, property2: 2 } }
GONG

就我而言,我将组件拼写错误。因此,我得到了这个错误。
mehul9595

57

只需Vue.component()在之前定义即可new vue()

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

new Vue({
    .......
});

更新

  • HTML转换<anyTag><anytag>
  • 因此,请勿使用大写字母作为组件名称
  • 代替<myTag>使用<my-tag>

Github问题:https//github.com/vuejs/vue/issues/2308


2
这应该是该问题的主要答案。我可以从vuejs文档中进行确认,并尝试将Vue.component放置根目录(或new Vue({}))之前。
法比安

2

这为我解决了问题:我提供了第三个参数作为对象。

app.js(使用laravel和webpack):

Vue.component('news-item', require('./components/NewsItem.vue'), {
    name: 'news-item'
});


1

确保已将组件添加到组件中。

例如:

export default {
data() {
    return {}
},
components: {
    'lead-status-modal': LeadStatusModal,
},
}

0

这是在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

我一直在关注Vue文档,网址https://vuejs.org/v2/guide/index.html遇到此问题时,我。

后来他们阐明了语法:

到目前为止,我们仅使用Vue.component在全球范围内注册了组件:

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

全局注册的组件可以 在之后创建的任何根Vue实例(新Vue)的模板中使用,甚至可以在该Vue实例的组件树的所有>子组件内使用。

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

因此,正如Umesh Kadam所说,只需确保全局组件定义在var app = new Vue({})实例化之前即可。


0

我有同样的错误

[Vue警告]:未知的自定义元素:-您是否正确注册了组件?对于递归组件,请确保提供“名称”选项。

但是,我完全忘了运行npm install && npm run devjs文件。

也许这可以帮助像我这样的新手。


0

好的,这个错误似乎很明显,但是有一天我一直在寻找答案,只是发现我已经声明了2次组件。两次声明时,VueJS根本不抱怨,这让我发疯了,很明显,我在两者之间有很多代码,当我添加一个新组件时,我将声明放在顶部,而我也有一个接近底部。所以下次再找这个,可以节省很多时间

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.