前端多個(gè)vue項(xiàng)目公共組件的三種方法

學(xué)習(xí)地址

一、在一個(gè)文件夾中按住shift的同時(shí),點(diǎn)擊鼠標(biāo)右鍵,打開命令窗口

首先創(chuàng)建三個(gè)項(xiàng)目,main、test、common,其中common是公共組件,main和test引用了common包。

vue create main

vue create test

vue create common

二、common包入口

創(chuàng)建完成后刪除common包一些無用的東西,修改package.json,增加一行 "main": "index.js", 這表明index.js就是common包的入口文件,然后創(chuàng)建index.js文件。

index.js文件

index.js中需包含上面說的第三方組件初始化和公共組件的導(dǎo)出,本示例完整內(nèi)容如下

import WeVue from 'we-vue'

import 'we-vue/lib/style.css'

import Vue from 'vue'

import playerPicker from './src/components/playerPicker'? //導(dǎo)出的組件

Vue.use(WeVue)

export {

? playerPicker

}

三、common包的使用

分別進(jìn)入main和test,在控制臺(tái)輸入命令:

npm install ../common/

其中…/common/是common的相對(duì)路徑,這里也可以輸入絕對(duì)路徑。

這樣就將common這個(gè)工程以node_module的方式引入到main和test項(xiàng)目中了??梢哉J褂胏ommon在index.js中導(dǎo)出的組件了。

命令執(zhí)行完后,package.json里會(huì)多一條記錄

"common": "file:../common",

四、common包中組件的使用

引入公共組件 公共組件創(chuàng)建好了,需要引入,引入代碼和引入其他組件的方式一樣,代碼如下:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容