# yarn global add http-server # touch vue.config.js # mkdir src/mock # touch src/mock/list.json # cd mock # http-server . module.exports ={ devServer:{ proxy:{ '/list':{ target:'http://localhost:8081', pathRewrite:{ '/list': 'list.json' } } } } }
router.jsvue
# yarn add vue-router # touch src/router.js import Vue from 'vue' import VueRouter from 'vue-router' import pageA from 'a.vue' import pageB from 'b.vue' Vue.user(VueRouter); const routers=[{ path:'/', component: pageA }, { path:'/b', component: pageB }] let router=new VueRouter({routers}) export default router;
main.jsvue-router
import Vue from 'vue' import App from './App.vue' import './directive/n' import router from './router' Vue.config.productionTip = false new Vue({ router, render: h => h(App), }).$mount('#app')
因爲咱們在平常開發過程常常會遇到組建間通信,固然。vue.js中父子通訊,一般父組建經過props傳值,子組建經過自定義事件¥emit來觸發等,包括父孫通信使用provide / inject,但多業務模塊而且不一樣模塊間數據並不相干,經過拆vuex拆成多個module方便代碼維護;而且也會使各功能模塊高內聚低耦合
vuex
store.js
vue-cli
# yarn add vuex # touch src/store.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const state={ count:1 } const mutations={ increment(state) { state.count++ }, decrement(state) { state.count-- } } const actions={ increment: ({ commit }) => { commit('increment') }, decrement: ({ commit }) => { commit('decrement') } } export default new Vuex.Store({ state, mutations, actions })
main.js
json
import Vue from 'vue' import App from './App.vue' import './directive/n' import router from './router' import store from './store/index' Vue.config.productionTip = false new Vue({ router, store, render: h => h(App), }).$mount('#app')
a.vue
app
<template> <div class="page"> <div> vuex {{$store.state.count}}</div> <button @click="increment">增長</button> <button @click="decrement">刪減</button> </div> </template> <script> import { mapActions } from 'vuex' export default { methods:{ ...mapActions([ 'increment', 'decrement' ]) } } </script>
# mkdir src/store # mkdir src/store/modules # touch src/store/modules/a.js # touch src/store/modules/b.js # touch src/store/index.js
a.js
ide
const state = { count: 1 } const mutations = { add(state) { state.count++ }, reduce(state) { state.count-- } } const actions = { add: ({ commit }) => { commit('add') }, reduce: ({ commit }) => { commit('reduce') } } export default { namespaced: true, state, mutations, actions }
b.js
函數
]const state = { money: 1 } const mutations = { add(state) { state.money++ }, reduce(state) { state.money-- } } const actions = { add: ({ commit }) => { commit('add') }, reduce: ({ commit }) => { commit('reduce') } } export default { namespaced: true, state, mutations, actions }
index.js
spa
import Vue from 'vue' import Vuex from 'vuex' import money from './modules/b' import count from './modules/a' Vue.use(Vuex) export default new Vuex.Store({ modules: { money, count } })
vue頁面中使用
代理
<template> <div class="page"> <div> vuex {{$store.state.count.count}}</div> <button @click="add">增長</button> <button @click="reduce">減小</button> </div> </template> <script> import { mapActions } from 'vuex' export default { methods: { ...mapActions('count', [ 'add', 'reduce' ]) } } </script>
vue頁面
<template> <div class="page"> <div> vuex {{$store.state.count.count}}</div> <button @click="add(3)">增長</button> <button @click="reduce">減小</button> </div> </template> <script> import { mapActions } from 'vuex' export default { methods: { ...mapActions('count', [ 'add', 'reduce' ]) } } </script>
a.js
const state = { count: 10 } const mutations = { add(state, param) { state.count += param }, reduce(state) { state.count-- } } const actions = { add: ({ commit }, param) => { commit('add', param) }, reduce: ({ commit }) => { commit('reduce') } } export default { namespaced: true, state, mutations, actions }