03组件批量注册
全局组件注册
引入Vue实例对象
遍历全局组件文件夹
批量注册全局组件
import Vue from 'vue'
const requireContext = require.context(
'./globar',
true, //递归查找目录
/\.vue$/ //匹配规则
)
requireContext.keys().forEach((filename) => {
const componentConfig = requireContext(filename) //获取组件内容
Vue.component(
componentConfig.default.name || componentConfig.name,
componentConfig.default || componentConfig
)
})全局组件内容
在页面中使用全局组件
驼峰写法自动换成以
-间隔的大写字母换成小写
路由注册
路由在组件化之后,进行批量注册
例如·在users文件夹是关于用户所有的路由
在路由注册文件index.js中引入所有路由
同样使用到
require.context函数遍历当前路由文件夹对路由注册文件
index.js不进行操作将其他组件路由合并成数组
最后注册路由
在路由注册完成后,也可以动态的添加路由
全局路由守卫,添加跳转进度条
Last updated
Was this helpful?