首页 > 技术文章 > 自动引入一个文件夹下的所有vue文件,实现vue文件动态引入

guxuelong 2020-04-26 15:34 原文

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));//本行代码看似无用,却是样式文件能够成功引入的关键
});

 

推荐阅读