Vue使用vue.use注册全局组件

 1.在src/components下面新建xxx文件夹,在xxx文件下新建index.vue文件,此处为多个组件

2.在components文件下新建index.js文件,内容如下

引入需要注册的全局组件
import Icon from './Icon'
import FileUpload from './file-upload/upload'
import Table from './table'
import PermissionArea from './permission-area'
import SysSelect from '../app/sys-select'
import SysCodeName from '../app/sys-select/sys-code-name'
import ArchivesAnnex from '../app/archives-annex'
import AddPerson from '../app/add-person-jiazhaoye'
import ChartWarning from './chart-warning'


import selectableTag from './t-ui/selectable-tag/main'



const components = [
  // common
  SysSelect,
  SysCodeName,
  Icon,
  Table,
  PermissionArea,
  FileUpload,
  selectableTag,
  ArchivesAnnex,
  AddPerson,
  ChartWarning
]


// 定位insyall函数,函数内部循环注册组件
// Vue参数在使用Vue.use方法时自动传入
// eslint-disable-next-line no-unused-vars
const install = function (Vue, opts = {}) {
  components.forEach((component) => {
    Vue.component(component.name, component)
  })


}
默认导出install函数
export default {
  install
}

3.在index.js下使用该插件

import Vue from 'vue'

import CustomComponents from '@/components/common'
Vue.use(CustomComponents, {})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JavaScript使用Vue.jsVue.use方法是用于全局注册Vue插件的方法。它需要作为Vue实例的全局方法进行调用。 在Vue 2中,如果要使用Vue插件,需要先调用Vue.use方法来注册该插件。Vue.use方法会自动调用插件的install方法,并将Vue作为参数传递给install方法。 使用Vue.use的步骤如下: 1. 引入Vue和你要使用的插件。 2. 调用Vue.use方法并传入插件作为参数。 3. 在Vue实例或组件使用插件提供的功能。 例如,如果我们要使用vue-router插件,可以按照以下步骤来注册使用它: ```javascript // 引入Vuevue-router import Vue from 'vue'; import VueRouter from 'vue-router'; // 调用Vue.use方法注册vue-router插件 Vue.use(VueRouter); // 创建VueRouter实例并配置路由 const router = new VueRouter({ routes: [ // 路由配置 ] }); // 创建Vue实例,并将router配置到实例中 new Vue({ router, // 其他配置 }).$mount('#app'); ``` 在上述示例中,我们首先引入Vuevue-router,然后调用Vue.use方法来注册vue-router插件。接下来,我们创建了VueRouter实例并配置了路由。最后,在创建Vue实例时,我们将router配置到实例中,以便在应用程序中使用vue-router提供的路由功能。 相关问题: 1. Vue.use方法的作用是什么? 2. Vue.use方法与Vue.component方法有什么区别? 3. 如何自定义一个Vue插件并使用Vue.use进行全局注册? 4. 如何在Vue插件中暴露一些全局组件或指令? 5. Vue.use方法的原理是什么?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值