本節主要包含如下內容:javascript
首先在state文件夾中新建一個mutation_types.js用於存放要提交的動做,和一個index.js主要配置js。這裏主要是定義了兩個動做,一個叫TOOGLE_FOOTER用於控制底部bar的顯示與否,一個叫SELECTED_TAB用於控制底部tab選中的標籤。vue
在index.js中定義的狀態state和mutations也是這兩個相關的:java
mutation_types.js
git
export const TOGGLE_FOOTER = 'TOGGLE_FOOTER'; export const SELECT_TAB = 'SELECT_TAB';index.js
import Vue from 'vue' import Vuex from 'vuex' import * as types from './mutation-types'//導入 Vue.use(Vuex); const store = new Vuex.Store({ state: { footerVisible: true,//底部bar顯示的狀態 selectedTab: 'main'//選中的tab標籤 }, mutations: { [types.TOGGLE_FOOTER] (state) { state.footerVisible = !state.footerVisible }, [types.SELECT_TAB](state, val){ state.selectedTab = val } } }); export default store接着在main.js中引入狀態管理:
import Vue from 'vue' import App from './App' import router from './router' import MintUI from 'mint-ui' import store from './store/index.js'//引入 import echarts from 'echarts' Vue.prototype.$echarts = echarts Vue.use(MintUI); /*Vue.config.productionTip = false;*/ new Vue({ el: '#app', router, store,//使用 template: '<App/>', components: {App} });接下來咱們的頁面要想使用這些狀態或者想要改變這些狀態,只須要 this.$store. state. footerVisible 或者 this.$store. commit( 'TOGGLE_FOOTER'); 就能夠了。
首頁的實現很簡單,就是一個header和一個能夠右滑菜單的cell,這些都是mint裏面的組件,能夠直接使用,先上代碼:github
<template> <div id="index"> <mt-header fixed title="首頁"></mt-header> <div class="content"> <mt-cell-swipe :right="right" title="未讀通知"> <span><mt-badge type="error">10</mt-badge></span> </mt-cell-swipe> </div> </div> </template> <style scoped> .content { margin-top: 40px; } </style> <script> import {Toast} from 'mint-ui'; export default { data(){ return { right: [ { content: '刪除', style: {background: 'red', color: '#fff', width: '50px', textAlign: 'center'}, handler: () => this.$messagebox({ title: '提示', message: '肯定執行此操做?', showCancelButton: true }).then((action) => { if (action === 'confirm') { Toast({message: '刪除成功'}) } else { } }) } ] } }, created(){ let _footer = this.$store.state.footerVisible; if (!_footer) { this.$store.commit('TOGGLE_FOOTER'); } this.$store.commit('SELECT_TAB', 'main') } } </script>能夠看到,在鉤子函數created中,獲取了當前footerbar的顯示狀態,若是是false的話,就變成true顯示;接着將選中的tab標籤設置爲main。
另外,mt-cell-swipe組件的屬性right接收一個對象,其中content表示右滑後顯示的內容,style表示content的樣式,handler()表示點擊content對應內容後的處理函數,這裏是彈出一個信息對話框,而後根據選擇「確認」「取消」來執行不一樣的動做。本例只是簡單的模擬一下各控件的使用,不涉及數據操做。vuex
2018-07-12更新:因爲後續增長和改進的東西比較多,強烈建議參考github上最新的項目:
https://github.com/JerryYuanJ/a-vue-app-template
若是你項目搭建中遇到問題,請提交issue或者及時與我聯繫,謝謝。