简体   繁体   English

如何将 Vue.component 与模块或 Vue CLI 一起使用?

[英]How do I use Vue.component with modules or Vue CLI?

I'm stuck on how to declare Vue.component inside export default我被困在如何在导出默认值中声明 Vue.component

this is from the tutorial by vuejs.org这是来自vuejs.org的教程

在此处输入图像描述

instead of using var app = new vue , I use而不是使用var app = new vue ,我使用

export default {
  name: "App",
  
  el: "#app-7",
  data() {
    return {
      barangBelanjaan: [
        { id: 0, barang: 'Sayuran' },
        { id: 1, barang: 'Keju' },
        { id: 2, barang: 'Makanan yang lain' }
      ],
    };
  },
};

and i dont know where to write Vue.component in export default app而且我不知道在导出默认应用程序中在哪里编写 Vue.component

thank you in advanced!提前谢谢你!

Components can be registered globally or locally.组件可以在全局或本地注册。 Vue.component is the way to register globally, which means all other components can then use this component in their templates. Vue.component是全局注册的方式,这意味着所有其他组件都可以在其模板中使用该组件。

Global components全局组件

When using a build tool like Vue CLI, do this in main.js :使用 Vue CLI 之类的构建工具时,请在main.js中执行此操作:

import Vue from 'vue'
import todoItem from '@/components/todoItem.vue'  // importing the module

Vue.component('todoItem', todoItem);              // ✅ Global component

-or- -或者-

Local components本地组件

Or you can register a component in a specific component using the components option.或者,您可以使用components选项在特定组件中注册组件。

components: {
  todoItem
}

So your App.vue would become:所以你的App.vue会变成:

import todoItem from '@/components/todoItem.vue'  // importing the module

export default {
  name: "App",
  el: "#app-7",
  components: {      // ✅ Local components
    todoItem
  },
  data() {
    return {
      barangBelanjaan: [
        { id: 0, barang: 'Sayuran' },
        { id: 1, barang: 'Keju' },
        { id: 2, barang: 'Makanan yang lain' }
      ],
    };
  },
}

Various recording options are possible, for example各种记录选项是可能的,例如

components: {      
    todoItem:() => import("@/components/todoItem.vue")
}

export default {
  name: "App",
  el: "#app-7",
  components: {      
    todoItem:() => import("@/components/todoItem.vue")
  },
  data() {
    return {
      barangBelanjaan: [
        { id: 0, barang: 'Sayuran' },
        { id: 1, barang: 'Keju' },
        { id: 2, barang: 'Makanan yang lain' }
      ],
    };
  },
}

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

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