我正在嘗試將一個 Vue/Vuex 專案分解為一個私人托管的 npm 包。我想我已經到了那里,但是我不確定我當前的布局,到目前為止我有:
src/
├── BrokerProposal
│ ├── FormInputs
│ ├── index.js
│ └── modals
└── store
├── index.js
└── modules
└── proposal
├── actions
├── getters
├── helpers
├── mutations
└── states
└── validations
我的目標是使 BrokerProposal 目錄可匯入,這是通過第一個index.js檔案完成的:
const BrokerProposal = require('./BrokerCalculator.vue');
function install(Vue) {
if (install.installed) return;
install.installed = true;
Vue.component("v-broker-proposal", BrokerProposal);
}
const plugin = {
install
};
let GlobalVue = null;
if (typeof window !== "undefined") {
GlobalVue = window.Vue;
} else if (typeof global !== "undefined") {
GlobalVue = global.vue;
}
if (GlobalVue) {
GlobalVue.use(plugin);
}
BrokerProposal.install = install;
export default BrokerProposal;
這個專案也使用了 vuex,所以我將狀態的 mutators 等分解到這個包中以配合 BrokerProposal,最終用戶可以在匯入后系結這個 store,這是 index.js 檔案:
import Vue from 'vue'
import Vuex from 'vuex'
// Vuex Modules
import tabs from './modules/tabs'
import proposal from './modules/proposal'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
tabs,
proposal,
},
})
export default store
我覺得我應該在同一級別包含另一個 index.js 檔案,因為/srcpackage.json 檔案中的“main”部分必須指向某些內容?
uj5u.com熱心網友回復:
應該避免類似的副作用Vue.use(Vuex),GlobalVue.use(plugin)因為它們可能會干擾使用此包的專案。使用適當的 Vue 實體設定插件是專案的責任。
所有公共匯出都可以在入口點命名為匯出,例如src/index.js:
export { default as BrokerProposal } from './BrokerProposal';
export { default as store } from './store';
此外,匯出組件也是一種很好的做法,以防它們需要在本地匯入,而不是依賴Vue.component.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403392.html
標籤:
上一篇:VuejS:輸入自動填充
