vue 將components文件夾中高頻公共組件,注冊成全局組件


1.在components文件夾下新建一個test.vue,global.js代碼如下:

import Vue from 'vue'

function changeStr (str) {
  return str.charAt(0).toUpperCase() + str.slice(1)
}

const requireComponent = require.context('./', false, /\.vue$/)
export default function install () {
  requireComponent.keys().forEach(fileName => {
    // 組件中信息
    let config = requireComponent(fileName)
    console.log('config:', config)
    // 定義組件名
    let componentName = changeStr(
      fileName.replace(/^\.\//, '').replace(/\.\w+$/, '')
    )
    console.log('componentName', componentName)
    Vue.component(fileName, config.default || config)
  })
}

2.在main.js中引入global.js

import install from './components/global'

install()

3.在.vue文件中可以直接引用

<test></test>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM