首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在模块或Vue CLI中使用Vue.component?

如何在模块或Vue CLI中使用Vue.component?
EN

Stack Overflow用户
提问于 2021-02-01 03:46:49
回答 2查看 818关注 0票数 1

我一直在讨论如何在导出默认情况下声明Vue.component

这是来自vuejs.org的教程

我没有使用var app = new vue,而是使用

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

我不知道在导出默认应用程序中应该在哪里编写Vue.component

谢谢你的进阶!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-01 04:17:08

组件可以在全球或本地注册。Vue.component是全局注册的方式,这意味着所有其他组件都可以在其模板中使用该组件。

全局组件

当使用像Vue CLI这样的构建工具时,请在main.js中这样做

代码语言:javascript
复制
import Vue from 'vue'
import todoItem from '@/components/todoItem.vue'  // importing the module

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

-或者-

局部成分

也可以使用components选项在特定组件中注册组件。

代码语言:javascript
复制
components: {
  todoItem
}

所以你的App.vue会变成:

代码语言:javascript
复制
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' }
      ],
    };
  },
}
票数 1
EN

Stack Overflow用户

发布于 2021-02-01 09:39:55

例如,各种录制选项是可能的。

代码语言:javascript
复制
components: {      
    todoItem:() => import("@/components/todoItem.vue")
}
代码语言:javascript
复制
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' }
      ],
    };
  },
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65986927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档