简体   繁体   English

Vue.js 导入子组件成组件

[英]Vue.js import child component into a component

Today I am making an todo web app, but now I want to import a Todo in my Todolist component.今天我正在制作一个 todo web 应用程序,但现在我想在我的Todolist组件中导入一个Todo My components:我的组件:

App (Parent) --> Todolist (Child) --> Todo (Child)应用(父)--> Todolist(子)--> Todo(子)

Error:错误:

24:5 error The "Todo" component has been registered but not used vue/no-unused-components 24:5 错误“Todo”组件已注册但未使用 vue/no-unused-components

Code代码

Todolist待办事项列表

<template>
<!-- TEMPLATE FOR THE TODOS -->

  <div v-for="(todo) in todos" :key="todo.id">
    <Todo
      :todo="todo.text"
      :index="todo.id"
      :checked="todo.checked"
      :deleteTodo="deleteTodo"
    />
  </div>

</template>
<script>
import Todo from './Todo'

export default {
  name: 'Todolist',
  props: {
    todos: Array
  }
}
</script>

The command line is telling me that the import has been imported, but the import has not been used yet.命令行告诉我导入已导入,但导入尚未使用。

Hope you guys can help me out,希望大家帮帮我

Thanks in advance!提前致谢!

Importing the child component's export is the first step, now its available to the current module.导入子组件的导出是第一步,现在它可用于当前模块。 But you still need to register that imported component in the parent using the components option:但是您仍然需要使用components选项在父级中注册该导入的组件:

Parent家长

import Todo from './Todo'

export default {
  name: 'Todolist',
  props: {
    todos: Array
  },
  components: {  // ✅ add this
    Todo
  }
}

It is possible to import a component dynamically, registration, as described above, is still required.可以动态导入组件,如上所述,仍然需要注册。

Use instead:改用:

import Todo from './Todo'

this option这个选项

const Todo = () => import('./Todo')

The component will be loaded only when needed.只有在需要时才会加载该组件。

or或者

You can add a component by importing and registering in components您可以通过在组件中导入和注册来添加components

components: {            
            Todo: () => import('./Todo'),
        },

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

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