vue,vuex,vue-router放在一塊兒能作什麼?不用webpack之類的打包工具使用他們是否可行?各位道友在初學vue時是否有這樣的困惑。由於現代構建前端項目的通常模式是:html
在這個過程當中你是否發現開發者愈來愈依賴構建工具,你是否想過構建工具幫助咱們解決了不少問題,但他也讓你不能快速的感知和了解庫或者框架的本質。此次就簡單點,用vue,vuex,vue-router,但不用webpack作一個示例,主要目的皆在瞭解這些庫的本質,或者說是是基礎應用。前端
本示例利用vue-router作爲導航,其中結合了vue和vuex相關知識,若是你想單獨瞭解其中的某一個庫,可訪問vue原來能夠這樣上手和vuex原來能夠這樣上手這兩個連接。若是你還想和咱們一塊兒討論前端技術,能夠加入本人建立的QQ羣,羣號在左側。單擊下載示例源碼vue
如下截取的都是代碼片段,或者是減小後的代碼,只表其意。如須要看完整的仍是下載示例源碼看吧。node
js代碼:webpack
var routeropt = [ { path: '', component: form }, { path: '/form', component: form}, { path: '/comp', component: comp, children:[ { path: '', component: compA }, { path: 'compA', component: compA }, { path: 'compB', component: compB } ]}, { path: '/life', component: compLife } ]; var router = new VueRouter({ routes: routeropt });
html代碼:es6
<div id="app"> <ul class="nav nav-tabs"> <li><router-link to="/form">選擇下拉列表</router-link></li> <li><router-link to="/comp">子組件路由</router-link></li> <li><router-link to="/life">生命週期</router-link></li> </ul> <router-view></router-view> </div>
說明:web
var state = { list: [{"id":1, "name": "001"}] }; var mutations = { ADDITEM: function(argState, item){ argState.list.push(item); } }; var getters = { getList:function(argState){ return argState.list; } } var actions = { addItem:function(dis,item){ dis.commit('ADDITEM',item); } }
createElement('button',{ on:{ "click": function(event){ self.$store.dispatch('addItem',{"id":2,"name": self.value}); } },
createElement("ul", { class:{ "dropdown-menu":true }, attrs:{ "aria-labelledby":"dr02" } }, self.$store.getters["getList"].map(function(item){ return createElement("li",item.name); }))
compB組件中的代碼:vue-router
computed: { list: function(){ return this.$store.getters.getList; } }
beforeCreate: function(){ var self = this, obj = {eventId: index++, eventName: 'beforeCreate--nextTick'}; this.$nextTick(function(){ self.addItem(obj); }); }, created: function(){ this.addItem({eventId: index++, eventName: 'created-----------------------'}); this.title = '生命週期'; }, beforeMount: function(){ this.addItem({eventId: index++, eventName: 'beforeMount'}); }, mounted: function(){ this.addItem({eventId: index++, eventName: 'mounted'}); }, activated: function(){ //keep-alive激活時 this.addItem({eventId: index++, eventName: 'activated'}); }, deactivated: function(){ this.addItem({eventId: index++, eventName: 'deactivated'}); }, beforeDestroy: function(){ this.addItem({eventId: index++, eventName: 'beforeDestroy'}); }, destroyed: function(){ this.addItem({eventId: index++, eventName: 'destroyed'}); }, beforeRouteEnter: function(to, from, next){ var obj = {eventId: index++, eventName: 'router: beforeRouteEnter--nextTick'}; next(function(vm){ vm.addItem(obj); }) }, beforeRouteUpdate: function(to, from, next){// 路由修改時 V2.2 this.addItem({eventId: index++, eventName: 'router: beforeRouteUpdate'}); next(true); }, beforeRouteLeave: function(to, from, next){ //路由離開當前組件時 this.addItem({eventId: index++, eventName: 'router: beforeRouteLeave'}); next(true); }
beforeUpdate: function(){ //最後一次修改渲染到DOM上數據的機會,不會致使重複執行渲染,而updated中修改狀態會致使重複渲染 //但在beforeUpdate中修改 $store,或者是$emit 來通知改變非本組件的VNODE,都會致使重複渲染 this.msg = '我不致使重複渲染'; }