.vue?組件打包成?.js的操作方法
.vue 組件打包成 .js
*** 所有的內(nèi)容 cli 官網(wǎng)都有 ***
*** https://cli.vuejs.org/zh/guide/build-targets.html ***
所有的內(nèi)容 cli 官網(wǎng)都有:
https://cli.vuejs.org/zh/guide/build-targets.html
準(zhǔn)備 幾個(gè) .vue 組件文件
import Main from './components/Main.vue' import Home from './views/Home.vue' import About from './views/About.vue' import _Vue from 'vue' [Main,Home,About].map(item=>{ item.install = Vue => { if (!Vue) { window.Vue = Vue = _Vue } Vue.component(item.name, item) } }) export { Main,Home,About }
vue.config.js 中新增: css: { extract: false }
:
提取出來(lái)的 CSS 文件 (可以通過(guò)在 vue.config.js 中設(shè)置 css: { extract: false } 強(qiáng)制內(nèi)聯(lián))package.json 中新增 命令
"lib": "vue-cli-service build --target lib --inline-vue --name myLib src/index.js"
npm run lib
打包后dist里面有demo ,可用于參考使用 index.html 入口文件中 引入 myLib.umd.min.js組件中
如果 vue.config.js 中新增了屬性
configureWebpack: config => { return { output: { libraryExport: 'default' // 默認(rèn)為 undefined,將會(huì)導(dǎo)出整個(gè)(命名空間)對(duì)象 }, } },
那么 index.js 文件就要改寫為:
import Main from './components/Main.vue' import Home from './views/Home.vue' import About from './views/About.vue' import _Vue from 'vue' [Main,Home,About].map(item=>{ item.install = Vue => { if (!Vue) { window.Vue = Vue = _Vue } Vue.component(item.name, item) } }) export default { // 這里有修改 Main,Home,About }
使用方法不變
9. 如果不想生成 source map 文件,可以在 vue.config.js 中新增
module.exports = { configureWebpack: config => { return { output: { libraryExport: 'default' // 默認(rèn)為 undefined,將會(huì)導(dǎo)出整個(gè)(命名空間)對(duì)象 }, devtool: 'none' // (none)(省略 devtool 選項(xiàng)) - 不生成 source map。webpack官網(wǎng)查詢 } }, css: { extract: false }, }
后續(xù)補(bǔ)充,不接上文
import Vue from 'vue' import App from './App.vue' import router from './router' import store from './store' Vue.config.productionTip = false window.Vue = Vue require('./assets/gx.js') // 這里不能用 import // gx.js 是 通過(guò) npx vue-cli-service build --target wc --name gx 'src/components/*/*.vue' 指令生成 /* <gx-cus_com-v1></gx-cus_com-v1> <gx-cus_com-v2></gx-cus_com-v2> <gx-log_com-v1></gx-log_com-v1> <gx-log_com-v2></gx-log_com-v2> */ new Vue({ router, store, render: h => h(App) }).$mount('#app')
文件樹是這樣的
到此這篇關(guān)于.vue 組件打包成 .js的文章就介紹到這了,更多相關(guān).vue 組件打包成 .js內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Vue中實(shí)現(xiàn)地圖熱點(diǎn)展示與交互的方法詳解(如熱力圖)
隨著大數(shù)據(jù)和可視化技術(shù)的發(fā)展,地圖熱點(diǎn)展示越來(lái)越受到人們的關(guān)注,在Vue應(yīng)用中,我們通常需要實(shí)現(xiàn)地圖熱點(diǎn)的展示和交互,以便更好地呈現(xiàn)數(shù)據(jù)和分析結(jié)果,本文將介紹在Vue中如何進(jìn)行地圖熱點(diǎn)展示與交互,包括熱力圖、點(diǎn)聚合等2023-07-07vue3中g(shù)etCurrentInstance示例講解
這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance的相關(guān)資料,文中還介紹了Vue3中關(guān)于getCurrentInstance的大坑,需要的朋友可以參考下2023-03-03如何解決npm i下載依賴的時(shí)候出現(xiàn)某依賴版本沖突
這篇文章主要介紹了如何解決npm i 下載依賴的時(shí)候出現(xiàn)某依賴版本沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue?目錄樹的展開與關(guān)閉的實(shí)現(xiàn)
Vue作為一款流行的前端框架,提供了一種數(shù)據(jù)驅(qū)動(dòng)的方式來(lái)實(shí)現(xiàn)目錄樹,本文主要介紹了vue?目錄樹的展開與關(guān)閉的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11uniapp使用條件編譯#ifdef(跨平臺(tái)設(shè)備兼容)
這篇文章主要介紹了uniapp使用條件編譯#ifdef(跨平臺(tái)設(shè)備兼容),需要的朋友可以參考下2022-12-12el-table實(shí)現(xiàn)轉(zhuǎn)置表格的示例代碼(行列互換)
這篇文章主要介紹了el-table實(shí)現(xiàn)轉(zhuǎn)置表格的示例代碼(行列互換),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02vite項(xiàng)目配置less全局樣式的實(shí)現(xiàn)步驟
最近想實(shí)現(xiàn)個(gè)項(xiàng)目,需要配置全局less,本文主要介紹了vite項(xiàng)目配置less全局樣式的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02