const context = require.context('./', true, /\.vue$/); const install = (Vue) => { context.keys().forEach((key) => { const component = context(key).default; Vue.component(component.name, component); }); };
Css、Sass等樣式文件同樣可以做到
const context = require.context('./', true, /\.scss$/); context.keys().forEach((key) => { // eslint-disable-next-line console.log(context(key));//本行代碼看似無用,卻是樣式文件能夠成功引入的關鍵 });