VUE组件加载

Posted on 2020-01-13 11:35  勤勤恳恳大猿人  阅读(361)  评论(0编辑  收藏  举报

今天,无意打开vue-cli 3.0 UI控制台

 

 

闲来无事,看看了源码学到一手组件的动态加载,分享大家

/**
 * We register all the components so future cli-ui plugins
 * could use them directly
 */

import Vue from 'vue'

// https://webpack.js.org/guides/dependency-management/#require-context
const requireComponent = require.context('./components', true, /[a-z0-9]+\.(jsx?|vue)$/i)

// For each matching file name...
requireComponent.keys().forEach(fileName => {
  const componentConfig = requireComponent(fileName)
  const componentName = fileName
    .substr(fileName.lastIndexOf('/') + 1)
    // Remove the file extension from the end
    .replace(/\.\w+$/, '')
  // Globally register the component
  Vue.component(componentName, componentConfig.default || componentConfig)
})
© 2020 GitHub, Inc.