【问题标题】:how to import many vue component to my vue page?如何将许多 vue 组件导入我的 vue 页面?
【发布时间】:2021-03-16 06:26:41
【问题描述】:

我不想写很多import from。

import button1 from './components/button1'
import button2 from './componnets/button2'
import table1 from './componnets/table2'
...

有什么好方法可以快速完成吗? 这件事有多少种方法?

【问题讨论】:

    标签: vue.js ecmascript-6 vuejs2


    【解决方案1】:

    按照这种模式,您也可以动态导入组件:

    computed: {
      comp () {
          return () => import(`@/components/${this.componentName}.vue`)
      }
    }
    

    然后像这样使用它:

    <template>
        <component :is="comp"></component>
    </template>
    

    【讨论】:

      【解决方案2】:

      您可以在索引文件中全局导入和注册 Vue 组件:

      import button1 from './components/button1'
      
      Vue.component('button1', button1);
      

      更多信息请查看官方文档:https://vuejs.org/v2/guide/components-registration.html#Global-Registration

      【讨论】:

      • 我不想全局化组件,只想将它们导入一个视图页面。
      【解决方案3】:

      你可以试试require.context,看看the official documentation中的例子,这应该足以解决你的问题。有关require.context 的更多信息,请参阅this question

      【讨论】:

        猜你喜欢
        • 2017-05-18
        • 2022-01-16
        • 2018-08-26
        • 2020-05-06
        • 2019-01-17
        • 2019-04-23
        • 2017-11-25
        • 2020-12-15
        • 2019-03-08
        相关资源
        最近更新 更多