中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

建一個(gè)做筆記的網(wǎng)站提高工作效率

建一個(gè)做筆記的網(wǎng)站,提高工作效率,網(wǎng)絡(luò)口碑營(yíng)銷的特點(diǎn),京東聯(lián)盟網(wǎng)站推廣位怎么做了解vuex核心概念請(qǐng)移步 https://vuex.vuejs.org/zh/ # 一、初始vuex # 1.1 vuex是什么 就是把需要共享的變量全部存儲(chǔ)在一個(gè)對(duì)象里面,然后將這個(gè)對(duì)象放在頂層組件中供其他組件使用 父子組件通信時(shí),我們通常會(huì)采用 props emit 這種方式。但當(dāng)通信雙方不…

了解vuex核心概念請(qǐng)移步 https://vuex.vuejs.org/zh/

img

# 一、初始vuex

# 1.1 vuex是什么

就是把需要共享的變量全部存儲(chǔ)在一個(gè)對(duì)象里面,然后將這個(gè)對(duì)象放在頂層組件中供其他組件使用

父子組件通信時(shí),我們通常會(huì)采用 props + emit 這種方式。但當(dāng)通信雙方不是父子組件甚至壓根不存在相關(guān)聯(lián)系,或者一個(gè)狀態(tài)需要共享給多個(gè)組件時(shí),就會(huì)非常麻煩,數(shù)據(jù)也會(huì)相當(dāng)難維護(hù)

# 1.2 vuex中有什么

const store = new Vuex.Store({state: {name: 'weish',age: 22},getters: {personInfo(state) {return `My name is ${state.name}, I am ${state.age}`;}}mutations: {SET_AGE(state, age) {commit(age, age);}},actions: {nameAsyn({commit}) {setTimeout(() => {commit('SET_AGE', 18);}, 1000);}},modules: {a: modulesA}
}

這個(gè)就是最基本也是完整的vuex代碼;vuex 包含有五個(gè)基本的對(duì)象

  • state:存儲(chǔ)狀態(tài)。也就是變量;
  • getters:派生狀態(tài)。也就是set、get中的get,有兩個(gè)可選參數(shù):state、getters分別可以獲取state中的變量和其他的getters。外部調(diào)用方式:store.getters.personInfo()。就和vuecomputed差不多;
  • mutations:提交狀態(tài)修改。也就是set、get中的set,這是vuex中唯一修改state的方式,但不支持異步操作。第一個(gè)參數(shù)默認(rèn)是state。外部調(diào)用方式:store.commit('SET_AGE', 18)。和vue中的methods類似。
  • actions:和mutations類似。不過(guò)actions支持異步操作。第一個(gè)參數(shù)默認(rèn)是和store具有相同參數(shù)屬性的對(duì)象。外部調(diào)用方式:store.dispatch('nameAsyn')。
  • modulesstore的子模塊,內(nèi)容就相當(dāng)于是store的一個(gè)實(shí)例。調(diào)用方式和前面介紹的相似,只是要加上當(dāng)前子模塊名,如:store.a.getters.xxx()

# 1.3 vue-cli中使用vuex的方式

目錄結(jié)構(gòu)

├── index.html
├── main.js
├── components
└── store├── index.js          # 我們組裝模塊并導(dǎo)出 store 的地方├── state.js          # 跟級(jí)別的 state├── getters.js        # 跟級(jí)別的 getter├── mutation-types.js # 根級(jí)別的mutations名稱(官方推薦mutions方法名使用大寫)├── mutations.js      # 根級(jí)別的 mutation├── actions.js        # 根級(jí)別的 action└── modules├── m1.js         # 模塊1└── m2.js         # 模塊2

state示例

const state = {name: 'weish',age: 22
};
export default state;

getter示例

getters.js示例(我們一般使用getters來(lái)獲取state的狀態(tài),而不是直接使用state

export const name = (state) => {return state.name;
}
export const age = (state) => {return state.age
}
export const other = (state) => {return `My name is ${state.name}, I am ${state.age}.`;
}

mutation-type示例

將所有mutations的函數(shù)名放在這個(gè)文件里

export const SET_NAME = ‘SET_NAME’;
export const SET_AGE = ‘SET_AGE’;

mutations示例

import * as types from ‘./mutation-type.js’;
export default {
[types.SET_NAME](state, name) {
state.name = name;
},
[types.SET_AGE](state, age) {
state.age = age;
}
};

actions示例

異步操作、多個(gè)commit時(shí)

import * as types from ‘./mutation-type.js’;
export default {
nameAsyn({commit}, {age, name}) {
commit(types.SET_NAME, name);
commit(types.SET_AGE, age);
}
}

modules–m1.js示例

如果不是很復(fù)雜的應(yīng)用,一般來(lái)講是不會(huì)分模塊的

export default {
state: {},
getters: {},
mutations: {},
actions: {}
}

index.js示例(組裝vuex)

import vue from ‘vue’;
import vuex from ‘vuex’;
import state from ‘./state.js’;
import * as getters from ‘./getters.js’;
import mutations from ‘./mutations.js’;
import actions from ‘./actions.js’;
import m1 from ‘./modules/m1.js’;
import m2 from ‘./modules/m2.js’;
import createLogger from ‘vuex/dist/logger’; // 修改日志
vue.use(vuex);
const debug = process.env.NODE_ENV !== ‘production’; // 開發(fā)環(huán)境中為true,否則為false
export default new vuex.Store({
state,
getters,
mutations,
actions,
modules: {
m1,
m2
},
plugins: debug ? [createLogger()] : [] // 開發(fā)環(huán)境下顯示vuex的狀態(tài)修改
});

最后將store實(shí)例掛載到main.js里面的vue上去就行了

import store from ‘./store/index.js’;
new Vue({
el: ‘#app’,
store,
render: h => h(App)
});

vue組件中使用時(shí),我們通常會(huì)使用mapGettersmapActions、mapMutations,然后就可以按照vue調(diào)用methodscomputed的方式去調(diào)用這些變量或函數(shù),示例如

import {mapGetters, mapMutations, mapActions} from ‘vuex’;
/* 只寫組件中的script部分 */
export default {
computed: {
…mapGetters([
name,
age
])
},
methods: {
…mapMutations({
setName: ‘SET_NAME’,
setAge: ‘SET_AGE’
}),
…mapActions([
nameAsyn
])
}
};

# 二、modules

在 src 目錄下 , 新建一個(gè) store 文件夾 , 然后在里面新建一個(gè) index.js

import Vue from ‘vue’
import vuex from ‘vuex’
Vue.use(vuex);
export default new vuex.Store({
state:{
show:false
}
})

main.js 里的代碼應(yīng)該改成,在實(shí)例化 Vue對(duì)象時(shí)加入 store 對(duì)象

//vuex
import store from ‘./store’
new Vue({
el: ‘#app’,
router,
store,//使用store
template: ‘<App/>’,
components: { App }
})

這樣就把 store 分離出去了 , 那么還有一個(gè)問(wèn)題是 : 這里 $store.state.show 無(wú)論哪個(gè)組件都可以使用 , 那組件多了之后 , 狀態(tài)也多了 , 這么多狀態(tài)都堆在 store 文件夾下的 index.js 不好維護(hù)怎么辦 ?

  • 我們可以使用 vuexmodules , 把 store 文件夾下的 index.js 改成
import Vue from ‘vue’
import vuex from ‘vuex’
Vue.use(vuex);
import dialog_store from ‘…/components/dialog_store.js’;//引入某個(gè)store對(duì)象
export default new vuex.Store({
modules: {
dialog: dialog_store
}
})

這里我們引用了一個(gè) dialog_store.js , 在這個(gè) js文件里我們就可以單獨(dú)寫 dialog 組件的狀態(tài)了

export default {
state:{
show:false
}
}

做出這樣的修改之后 , 我們將之前我們使用的 s t o r e . s t a t e . s h o w < / c o d e > 統(tǒng)統(tǒng)改為 < c o d e > store.state.show</code> 統(tǒng)統(tǒng)改為 <code> store.state.show</code>統(tǒng)統(tǒng)改為<code>store.state.dialog.show 即可

  • 如果還有其他的組件需要使用 vuex , 就新建一個(gè)對(duì)應(yīng)的狀態(tài)文件 , 然后將他們加入 store文件夾下的 index.js文件中的 modules
modules: {
dialog: dialog_store,
other: other,//其他組件
}

# 三、mutations

對(duì)vuex 的依賴僅僅只有一個(gè) $store.state.dialog.show 一個(gè)狀態(tài) , 但是如果我們要進(jìn)行一個(gè)操作 , 需要依賴很多很多個(gè)狀態(tài) , 那管理起來(lái)又麻煩了

  • mutations里的操作必須是同步的
export default {state:{//stateshow:false},mutations:{switch_dialog(state){//這里的state對(duì)應(yīng)著上面這個(gè)statestate.show = state.show?false:true;//你還可以在這里執(zhí)行其他的操作改變state}}
}

使用 mutations 后 , 原先我們的父組件可以改為

<template><div id="app"><a href="javascript:;" @click="$store.commit('switch_dialog')">點(diǎn)擊</a><t-dialog></t-dialog></div>
</template>
<script>
import dialog from './components/dialog.vue'
export default {components:{"t-dialog":dialog}
}
</script>

使用 $store.commit('switch_dialog') 來(lái)觸發(fā) mutations 中的 switch_dialog 方法

# 四、actions

多個(gè) state 的操作 , 使用 mutations會(huì)來(lái)觸發(fā)會(huì)比較好維護(hù) , 那么需要執(zhí)行多個(gè) mutations 就需要用 action

export default {state:{//stateshow:false},mutations:{switch_dialog(state){//這里的state對(duì)應(yīng)著上面這個(gè)statestate.show = state.show?false:true;//你還可以在這里執(zhí)行其他的操作改變state}},actions:{switch_dialog(context){//這里的context和我們使用的$store擁有相同的對(duì)象和方法context.commit('switch_dialog');//你還可以在這里觸發(fā)其他的mutations方法},}
}

那么 , 在之前的父組件中 , 我們需要做修改 , 來(lái)觸發(fā) action 里的 switch_dialog 方法

<template><div id="app"><a href="javascript:;" @click="$store.dispatch('switch_dialog')">點(diǎn)擊</a><t-dialog></t-dialog></div>
</template>
<script>
import dialog from './components/dialog.vue'
export default {components:{"t-dialog":dialog}
}
</script>
  • 使用 $store.dispatch('switch_dialog') 來(lái)觸發(fā) action 中的 switch_dialog 方法。
  • 官方推薦 , 將異步操作放在 action

# 五、getters

gettersvue 中的computed 類似 , 都是用來(lái)計(jì)算 state 然后生成新的數(shù)據(jù) ( 狀態(tài) ) 的

  • 假如我們需要一個(gè)與狀態(tài) show 剛好相反的狀態(tài) , 使用 vue 中的 computed 可以這樣算出來(lái)
computed(){not_show(){return !this.$store.state.dialog.show;}
}

那么 , 如果很多很多個(gè)組件中都需要用到這個(gè)與 show剛好相反的狀態(tài) , 那么我們需要寫很多很多個(gè) not_show, 使用 getters就可以解決這種問(wèn)題

export default {state:{//stateshow:false},getters:{not_show(state){//這里的state對(duì)應(yīng)著上面這個(gè)statereturn !state.show;}},mutations:{switch_dialog(state){//這里的state對(duì)應(yīng)著上面這個(gè)statestate.show = state.show?false:true;//你還可以在這里執(zhí)行其他的操作改變state}},actions:{switch_dialog(context){//這里的context和我們使用的$store擁有相同的對(duì)象和方法context.commit('switch_dialog');//你還可以在這里觸發(fā)其他的mutations方法},}
}

我們?cè)诮M件中使用 $store.state.dialog.show 來(lái)獲得狀態(tài) show , 類似的 , 我們可以使用 $store.getters.not_show 來(lái)獲得狀態(tài) not_show

  • 注意 : $store.getters.not_show 的值是不能直接修改的 , 需要對(duì)應(yīng)的 state 發(fā)生變化才能修改

# 六、mapState、mapGetters、mapActions

很多時(shí)候 , $store.state.dialog.show 、$store.dispatch('switch_dialog') 這種寫法很不方便

  • 使用 mapState、mapGettersmapActions 就不會(huì)這么復(fù)雜了
<template><el-dialog :visible.sync="show"></el-dialog>
</template>
<script>
import {mapState} from 'vuex';
export default {computed:{//這里的三點(diǎn)叫做 : 擴(kuò)展運(yùn)算符...mapState({show:state=>state.dialog.show}),}
}
</script>

相當(dāng)于

<template><el-dialog :visible.sync="show"></el-dialog>
</template>
<script>
import {mapState} from 'vuex';
export default {computed:{show(){return this.$store.state.dialog.show;}}
}
</script>

mapGetters、mapActionsmapState 類似 , mapGetters 一般也寫在 computed 中 , mapActions 一般寫在 methods

http://www.risenshineclean.com/news/21575.html

相關(guān)文章:

  • 購(gòu)物網(wǎng)站建設(shè)圖標(biāo)大全蘇州seo關(guān)鍵詞優(yōu)化軟件
  • 社交網(wǎng)站源代碼指數(shù)計(jì)算器
  • 虛擬主機(jī)和網(wǎng)站空間百度怎么推廣網(wǎng)站
  • 做網(wǎng)站電話銷售上海哪家seo好
  • 網(wǎng)站做快照深圳百度推廣seo公司
  • 網(wǎng)站上傳后后臺(tái)進(jìn)不去外鏈下載
  • 宜昌便宜做網(wǎng)站新聞最新消息
  • 用java編程做網(wǎng)站windows優(yōu)化大師的優(yōu)點(diǎn)
  • 免費(fèi)做網(wǎng)站公司哪家好百度公司官網(wǎng)入口
  • 寶雞做網(wǎng)站的公司網(wǎng)站平臺(tái)都有哪些
  • 垂直類b2c網(wǎng)站北京網(wǎng)優(yōu)化seo優(yōu)化公司
  • 網(wǎng)站 域名綁定google中文搜索引擎
  • 東莞網(wǎng)站排名優(yōu)化seoapp軟件推廣怎么做
  • 新鄉(xiāng)專業(yè)網(wǎng)站建設(shè)公司地推團(tuán)隊(duì)如何收費(fèi)
  • wordpress禁止評(píng)論優(yōu)化品牌seo關(guān)鍵詞
  • 如今做哪些網(wǎng)站致富百度站長(zhǎng)工具官網(wǎng)
  • 騰訊云服務(wù)器可以做傳奇網(wǎng)站嗎我想在百度上做廣告怎么做
  • 滄浪企業(yè)建設(shè)網(wǎng)站價(jià)格營(yíng)銷軟文范例大全100
  • 建設(shè)銀行網(wǎng)站怎么短信轉(zhuǎn)賬關(guān)鍵詞權(quán)重如何打造
  • 營(yíng)銷型網(wǎng)站建設(shè)極速建站seo推廣軟件排行榜前十名
  • 戶縣規(guī)劃建設(shè)和住房保障局網(wǎng)站沙坪壩區(qū)優(yōu)化關(guān)鍵詞軟件
  • wordpress還是hexo青島seo關(guān)鍵詞優(yōu)化公司
  • 建設(shè)一個(gè)網(wǎng)站app全過(guò)程seo權(quán)威入門教程
  • 自己做的網(wǎng)站涉黃網(wǎng)站怎么推廣
  • 成交型網(wǎng)站倡導(dǎo)公司西安百度網(wǎng)站快速排名
  • 成都網(wǎng)站建設(shè)是什么百度店面定位怎么申請(qǐng)
  • 阿里媽媽新建網(wǎng)站怎么做百度客服投訴中心
  • 嘉峪關(guān)市建設(shè)局建管科網(wǎng)站外鏈價(jià)格
  • 查看網(wǎng)站有沒(méi)有備案全國(guó)疫情防控最新數(shù)據(jù)
  • 手機(jī)上做整蠱網(wǎng)站全網(wǎng)推廣軟件