欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明

 更新時(shí)間:2022年04月02日 15:09:27   作者:拐鍋  
這篇文章主要介紹了Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

this.$store.commit()和this.$store.dispatch()的區(qū)別

兩個(gè)方法其實(shí)很相似,關(guān)鍵在于一個(gè)是同步,一個(gè)是異步

commit: 同步操作

this.$store.commit('方法名',值) //存儲(chǔ)
this.$store.state.'方法名' //取值

dispatch: 異步操作

this.$store.dispatch('方法名',值) //存儲(chǔ)
this.$store.getters.'方法名' //取值

當(dāng)操作行為中含有異步操作,比如向后臺(tái)發(fā)送請(qǐng)求獲取數(shù)據(jù),就需要使用action的dispatch去完成了,其他使用commit即可.

其他了解

  • commit => mutations, 用來觸發(fā)同步操作的方法.
  • dispatch => actions, 用來觸發(fā)異步操作的方法.

在store中注冊(cè)了mutation和action

在組件中用dispatch調(diào)用action,用commit調(diào)用mutation

Vuex應(yīng)用實(shí)例this.$store.commit()觸發(fā)

新建文件夾store,store下

action.js

const actions = {}
export default actions;

getter.js

const getters = {}
export default getters;

mutation-types.js

export const publicSetEvent = 'publicSetEvent';

mutations.js

import {publicSetEvent} from './mutation-types';
const mutations = {
? ? [publicSetEvent]: (state, json) => {
    // 初始化默認(rèn),避免跳轉(zhuǎn)路由時(shí)的公用部分顯示的相互影響
? ? ? ?state.publicSet = {headTitle: true,headNav: false,sTitle: '頭部標(biāo)題'}
// 是否顯示頭部title
? ? ? ? state.publicSet.headTitle = json.headTitle || state.publicSet.headTitle;
? ? ? ? // 是否顯示頭部tabbar切換
? ? ? ? state.publicSet.headNav = json.headNav || state.publicSet.headNav;
? ? ? ? // 頭部顯示的標(biāo)題文字
? ? ? ? state.publicSet.sTitle = json.sTitle || state.publicSet.sTitle;
? ? ? ? // tabbar的標(biāo)題文字及待辦badge數(shù)字
? ? ? ? state.publicSet.navList = json.navList || state.publicSet.navList;
? ? }
}
export default mutations;

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations';
import getters from './getters';
import actions from './actions';
Vue.use(Vuex);
const state = {
? ? publicSet: {//設(shè)置公共頭
? ? ? ? headTitle: true,
? ? ? ? headNav: false,
? ? ? ? sTitle: '頭部標(biāo)題'
? ? }
}
const store = new Vuex.Store({
? ? state,
? ? getters,
? ? mutations,
? ? actions
});
export default store;

頭部公共組件components文件夾下

v-header.vue

<template>
? <div class="v-header">
? ? <vTitle v-if="publicSet.headTitle" :stitle="publicSet.sTitle"></vTitle>
? </div>
</template>
<script>
import vTitle from './v-title';
import {mapState} from 'vuex';
export default{
? ?name:'v-header',
? ?components:{vTitle},
? ?data(){
? ? return{
? ? ??
? ? }
? ?},
? ?computed: {
? ? ? ?...mapState(['publicSet'])
? ?}
}
</script>

v-title.vue

<template>
? <div class="v-title">
? ? ? <XHeader :left-options="{backText:''}" :title="stitle"></XHeader>
? </div>
</template>
<script>
import { XHeader } from 'vux'
export default{
? name:'v-title',
? props:['stitle'],
? components:{XHeader},
? data (){
? ? ? return {
? ? ? }
? },
? methods: {
? }
}
</script>
<style lang="less">
</style>

App.vue

<template>
? <div id="app">
? ? <vHeader></vHeader>
? ? <router-view/>
? </div>
</template>
<script>
import vHeader from '@/components/header/v-header'
export default {
? name: 'app',
? components:{vHeader}
}
</script>

main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import store from './store'
Vue.use(Vuex)
Vue.config.productionTip = false
new Vue({
? el: '#app',
? router,
? store,
? components: { App },
? template: '<App/>'
})

頁面調(diào)用index.vue

<template>
? ? <div class="index">
? ? </div>
</template>
<script>
export default{
? ? name:'index',
? ? data(){
? ? ? ? return{
? ? ? ? }
? ? },
? ? created(){
? ? },
? ? beforeRouteEnter(to,from,next){
? ? ? ? let option={
? ? ? ? ? headTitle:true,
      sTitle:'我是新標(biāo)題'
? ? ? ? }
? ? ? ? console.log(option);
? ? ? ? next(vm=>{
? ? ? ? ? vm.$store.commit('publicSetEvent',option);
? ? ? ? })
? ? },
? ? methods:{
? ? }? ??
}
</script>
<style lang="less">
</style>

運(yùn)行進(jìn)去index頁面就可以看到公共頭了

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • Vue snippets插件原理與使用介紹

    Vue snippets插件原理與使用介紹

    這篇文章主要介紹了Vue snippets插件原理與使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • vue+iview的菜單與頁簽的聯(lián)動(dòng)方式

    vue+iview的菜單與頁簽的聯(lián)動(dòng)方式

    這篇文章主要介紹了vue+iview的菜單與頁簽的聯(lián)動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • vue中如何使用vue-touch插件

    vue中如何使用vue-touch插件

    這篇文章主要介紹了vue中使用vue-touch插件的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • vue使用xlsx庫(kù)和xlsx-style庫(kù)導(dǎo)入導(dǎo)出excel、設(shè)置單元格背景色、文字居中、合并單元格、設(shè)置列寬

    vue使用xlsx庫(kù)和xlsx-style庫(kù)導(dǎo)入導(dǎo)出excel、設(shè)置單元格背景色、文字居中、合并單元格、設(shè)置列寬

    xlsx是由SheetJS開發(fā)的一個(gè)處理excel文件的npm庫(kù),適用于前端開發(fā)者實(shí)現(xiàn)導(dǎo)入導(dǎo)出excel文件的經(jīng)典需求,這篇文章主要介紹了vue使用xlsx庫(kù)和xlsx-style庫(kù)導(dǎo)入導(dǎo)出excel、設(shè)置單元格背景色、文字居中、合并單元格、設(shè)置列寬,需要的朋友可以參考下
    2023-11-11
  • Vue.use與Vue.prototype的區(qū)別及說明

    Vue.use與Vue.prototype的區(qū)別及說明

    這篇文章主要介紹了Vue.use與Vue.prototype的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 關(guān)于Echarts餅圖圖例太長(zhǎng)的解決方案

    關(guān)于Echarts餅圖圖例太長(zhǎng)的解決方案

    這篇文章主要介紹了關(guān)于Echarts餅圖圖例太長(zhǎng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • vue中的封裝常用工具類

    vue中的封裝常用工具類

    這篇文章主要介紹了vue中的封裝常用工具類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue3 Element-plus el-menu無限級(jí)菜單組件封裝過程

    Vue3 Element-plus el-menu無限級(jí)菜單組件封裝過程

    對(duì)于element中提供給我們的el-menu組件最多可以實(shí)現(xiàn)三層嵌套,如果多一層數(shù)據(jù)只能自己通過變量去加一層,如果加了兩層、三層這種往往是行不通的,所以只能進(jìn)行封裝,這篇文章主要介紹了Vue3 Element-plus el-menu無限級(jí)菜單組件封裝,需要的朋友可以參考下
    2023-04-04
  • vue form check 表單驗(yàn)證的實(shí)現(xiàn)代碼

    vue form check 表單驗(yàn)證的實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue form check 表單驗(yàn)證的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-12-12
  • 運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決

    運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決

    剛剛創(chuàng)建好vue項(xiàng)目的時(shí)候,運(yùn)行 npm run dev 會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決方法,需要的朋友可以參考下
    2022-10-10

最新評(píng)論