標籤:讀取 pat 研究 har rar 類比 name 下載 相對
在五一節之前和一網友討論前端技術時,對方提到vue、vue-route如果配合requirejs應用。當時的我沒有想得很明白,也沒能這位網友一個準確的回複,但我確保於他五一研究後給他一個回複。本是一天的研究卻被我搞成了研究了一周,這拖延症。。。
閑話少數,進入正題
一、範例程式碼說明
代碼結構說明:
- modules(存放的為組件,主要是模板與Js對象分成兩個檔案來寫)
- route:測試子路由的組件
- title:就是一個簡單的顯示文字的組件
- app.js:核心類,提供vue的建立、以前modules組件載入的方法等
- chart.js:類比的一個業務模組
- index.html:分頁檔
- layout.css:整體樣式檔案(測試require加入樣式檔案)
- main.js:requirejs的設定檔(也是入口檔案)
醜陋的:
此樣本沒有樣式,只是為了驗證require如何載入一個vue組件,以子路由的動態注入的能力,範例程式碼下載。
二、從.vue檔案入手
一個.vue檔案可以包含模板、Js類、樣式(可以不要)等三塊。但我們通過vue的官網可以知道,vue提供了compile對象方法,可以把模板編譯成VNode。並且我們通過webpack打包後產生的檔案可以看出模板與Js類是混淆在一起了。這也就說明vue的組件就是一個Js對象。如所示:
三、requirejs對vue、vuex和vue-route的引入
引入這三個都很容易,並將這三個注入到Vue對象也是相對簡單的,難道的是需要解決動態注入向vue-route執行個體注入路由,以及vuex的動態加入一個資料模組的能力。好在這兩點官網都給出瞭解決方案:
- vue-route如何動態注入路由
根據官網協助文檔說明,存在addRoutes方法,向路由實體動態注入路由
- vuex模組動態注入
也是根據官網協助文檔提示有registerModule方法實現。
- 實現的部分代碼:
初始化Vue對象
apt.init = function(){ this.store = Object.create({ modules:{} }); this.Vue.use(VueRouter); this.Vue.use(Vuex); this.router = new VueRouter(); this.store = new this.Vuex.Store(this.store);}
首先提供一個init方法,對Vue對象進行一些初始化,也就是把Vuex、vue-route都注入到Vue對象中。在這裡我把建立的vuex和vue-route的執行個體都放到this對象,方便後面提供給組件註冊實使用。
建立Vue執行個體:
apt.createVue = function(){ this.vue = new this.Vue({ store: this.store, router: this.router, computed: { childs: function(){ if(!this.$store.state.router) return null; return this.$store.state.router.childs; } } }); return this.vue;}
只建立vue對象,沒有進行mount。
為其他模組提供的上下文:
apt.createContext = function(){ return { Vue: this.Vue, router: this.router, $vue: this.vue };}四、如何通過require載入html和js方式的組件
從項目結構圖中可以看出在modules檔案夾中定義了兩個組件,分別是:routet和title,而他們的模板則是一個html檔案。以下是這類組件如何載入的代碼:
apt.acquire = function(path){ var arrayPath; if(!this.isArray(path)){ arrayPath = [path]; }else{ arrayPath = path; } var promise = this.dfd(function(dfd){ require(arrayPath,function(){ dfd.resolve(Array.prototype.slice.call(arguments)); },function(error){ dfd.reject(error); }); }).promise(); return promise;}apt.createComponent = function(componentName){ //可以重載,讀取.vue的檔案 var path = this.$modulePrefix + componentName, html = ‘text!‘ + path + ‘/index.html‘, js = path + ‘/index.js‘, self = this; var promise = this.acquire([html,js]); promise.done(function(result){ var obj = result[1], content = result[0]; obj.template = content; obj.__path__ = path; self.$components.push(obj); }); return promise;}
說明: acquire:提供通過require載入JS或者是html等檔案的方法,並返回一個promise,這樣就方便調用者使用。 createComponet:會根據調用傳入的名稱在modules檔案夾中找出對應的js和html檔案,然後調用acquire載入組件。
五、main.js是這樣引用的
提供註冊全域群組件方法
apt.registerGlobalComponents = function(componentNames){ var gloadComponet = componentNames, self = this; var promises = gloadComponet.map(function(data,index){ return self.createComponent(data); }); var dfd = this.dfd(); $.when.apply(null, promises).done(function(){ var _router = []; self.$components.forEach(function(data,index){ self.Vue.component(data.name, data); _router.push({ path: ‘/‘ + data.name, component: data }); }); self.router.addRoutes(_router); //全域註冊都註冊為路由 dfd.resolve(_router); }); return dfd.promise();}
main.js中的引用
var _app = app.createApp();_app.registerGlobalComponents([‘title‘, ‘route‘]).done(function(){ var vue = _app.createVue(); var cxt = app.getVue().createContext(); var r = { state: { childs: [] }, mutations: { childs: function(state, data){ state.childs = data; } }, actions: { childs: function(state, data){ state.commit(‘childs‘, data); } } } vue.$store.registerModule(‘router‘, r); vue.$mount(‘#app‘);});
說明:
- 建立App的一個執行個體;
- 註冊全域的組件:title、route;
- 註冊完成後建立vue執行個體,並且向執行個體的vuex注入二級路由展示的模組
requirejs、vue、vuex、vue-route的結合使用,您認為可行嗎?