vuex實例

1,vue-cli創(chuàng)建項目
(1) npm install -g vue-cli
如果安裝失敗,可以使用npm cache clean 清理緩存,然后再重新安裝。
(2) vue -V 查看是否安裝成功
(3) 初始化項目vue init webpack vue-project

image.png

(4) 安裝vuex npm install vuex --save
(5) 在src目錄下新建文件夾store
image.png

(6) 路由配置 router---index.js

import Vue from 'vue'
import Router from 'vue-router'
import HomeA from '@/components/home/HomeA'
import HomeB from '@/components/home/HomeB'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HomeA',
      component: HomeA
    },
    {
      path: '/home/HomeA',
      name: 'HomeA',
      component: HomeA
    },
    {
      path: '/home/HomeB',
      name: 'HomeB',
      component: HomeB
    }
  ]
})

(7)
store.js

import Vue from 'vue'
import Vuex from 'vuex'
import * as getters from './getters' // 導(dǎo)入響應(yīng)的模塊,*相當于引入了這個組件下所有導(dǎo)出的事例
import * as actions from './actions'
import * as mutations from './mutations'

Vue.use(Vuex)

// 首先聲明一個需要全局維護的狀態(tài)state
const state = {
  newName: '新名字',
  id: '123'
}

// 注冊上邊引入的各大模塊
const store = new Vuex.Store({
  state, // 共同維護的一個狀態(tài),state里面可以是很多個全局狀態(tài)
  getters, // 獲取數(shù)據(jù)并渲染
  actions, // 數(shù)據(jù)的異步操作
  mutations// 處理數(shù)據(jù)的唯一途徑,state的改變和賦值都在這里
})

export default store
// 導(dǎo)出store,并在main.js中引用注冊

actions.js

// 給action注冊事件處理函數(shù),當這個函數(shù)被觸發(fā)時候,將狀態(tài)提交到mutations中處理
export function modifyAName ({commit}, name) {
  return commit('modifyAName', name)
}

// export function modifyBName ({commit}, name) {
//   return commit('modifyBName', name)
// }

export const modifyBName = ({commit}, name) => commit('modifyBName', name)

mutations.js

export const modifyAName = (state, name) => { // A組件點擊更改名稱為A
  state.newName = name // 把方法傳遞過來的參數(shù),賦值給state中的newName
}

export const modifyBName = (state, name) => {
  state.newName = name
}

getters.js

// 獲取最終的狀態(tài)信息
export const newName = state => state.newName

在main.js中導(dǎo)入store實例

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store, // 全局使用vuex
  components: { App },
  template: '<App/>'
})

(8)組件A

<template>
  <div class="componentsA">
    <P class="title">組件A</P>
    <P class="titleName">名稱:{{newName}}</P>
    <div>
      <!-- 點擊修改 為 A  -->
      <button class="btn" @click="modifyAName('AAA')">修改為AAAA</button>
    </div>
    <div class="marTop">
      <button class="btn" @click="trunToB">跳轉(zhuǎn)到B頁面</button>
    </div>
  </div>
</template>

<script>
import {mapActions, mapGetters} from 'vuex'
export default {
  name: 'HomeA',
  data () {
    return {
    }
  },
  methods: {
    ...mapActions( // 語法糖
      ['modifyAName'] // 相當于this.$store.dispatch('modifyName'),提交這個方法
    ),
    trunToB () {
      this.$router.push({path: '/home/HomeB'}) // 路由跳轉(zhuǎn)到B
    }
  },
  computed: {
    ...mapGetters(['newName']) // 動態(tài)計算屬性,相當于this.$store.getters.resturantName
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .title,.titleName{
    color: blue;
    font-size: 20px;
  }
  .btn{
    width: 160px;
    height: 40px;
    background-color: blue;
    border: none;
    outline: none;
    color: #ffffff;
    border-radius: 4px;
  }
  .marTop{
    margin-top: 20px;
  }
</style>

(9) 組件B

<template>
  <div class="componentsB">
    <P class="title">組件B</P>
    <P class="titleName">名稱:{{newName}}</P>
    <div>
      <!-- 點擊修改 為 B  -->
      <button class="btn" @click="modifyBName('BBB')">修改為BBBB</button>
    </div>
    <div class="marTop">
      <button class="btn" @click="trunToA">跳轉(zhuǎn)到A頁面</button>
    </div>
  </div>
</template>

<script>
import {mapActions, mapGetters} from 'vuex'
export default {
  name: 'HomeB',
  data () {
    return {
    }
  },
  methods: {
    ...mapActions( // 語法糖
      ['modifyBName'] // 相當于this.$store.dispatch('modifyName'),提交這個方法
    ),
    trunToA () {
      this.$router.push({path: '/home/HomeA'}) // 路由跳轉(zhuǎn)到A
    }
  },
  computed: {
    ...mapGetters(['newName']) // 動態(tài)計算屬性,相當于this.$store.getters.resturantName
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .title,.titleName{
    color: red;
    font-size: 20px;
  }
  .btn{
    width: 160px;
    height: 40px;
    background-color: red;
    border: none;
    outline: none;
    color: #ffffff;
    border-radius: 4px;
  }
  .marTop{
    margin-top: 20px;
  }
</style>
image.png

image.png

image.png

image.png

參考出處

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ## 框架和庫的區(qū)別?> 框架(framework):一套完整的軟件設(shè)計架構(gòu)和**解決方案**。> > 庫(lib...
    Rui_bdad閱讀 3,155評論 1 4
  • 如果你在使用 vue.js , 我想你可能會對 vue 組件之間的通信感到崩潰 。哪有沒有解決辦法呢? Vuex:...
    侯工閱讀 2,908評論 0 1
  • VUE Vue :數(shù)據(jù)驅(qū)動的M V Vm框架 m :model(后臺提供數(shù)據(jù)),v :view(頁面),vM(模板...
    wudongyu閱讀 5,534評論 0 11
  • 遇到她要從小學(xué)一年級開始,那個時候的記憶模模糊糊的,只記得那時候我是組長,她是班長,我整天都和同學(xué)們打打鬧鬧,...
    TKLover閱讀 334評論 0 1
  • 我是不喜歡看電視劇的,喜歡看文章,于是通過文章我看到了《我的前半生》 我起初是被賀涵這樣的精英人才所吸引,也希望能...
    二七女王閱讀 561評論 0 0

友情鏈接更多精彩內(nèi)容