- 在list页面----》“List页面吧啦啦不显示” 是隐藏状态,
- list跳转到EditeList页面,EditeList页面中------》“EditeList页面吧啦啦显示” 是显示状态。
- 再从EditeList页面跳转到List页面,List页面-----》“List页面吧啦啦不显示” 是隐藏状态
List页面:
<template>
<div class="list">
<h3>我是List页面</h3>
<p v-if="this.$store.state.showBack">List页面吧啦啦不显示</p>
<el-button type="primary" @click="goEditeListPage">跳转到EditeList页面</el-button>
</div>
</template>
<script>
export default {
name: 'List',
methods:{
goEditeListPage(){
this.$router.push({
name:'EditList'
})
this.$store.dispatch('setShowBack', true)
}
}
}
</script>
EditeList页面:
<template>
<div class="editList">
<h3>我是EditeList页面</h3>
<p v-if="this.$store.state.showBack" style="color:red;">EditeList页面吧啦啦显示</p>
<el-button type="primary" @click="goListPage">跳转到list页面</el-button>
</div>
</template>
<script>
export default {
name: 'EditList',
methods:{
goListPage(){
this.$router.push({
name:'List'
})
this.$store.dispatch('setShowBack', false)
}
}
}
</script>
以下是关于store的代码写法:
store/index.js:
import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import state from './state';
import mutations from './mutations';
import actions from './actions';
import getters from './getters';
Vue.use(Vuex)
const store = new Vuex.Store({
state,
mutations,
actions,
getters,
plugins:[
createPersistedState()
]
})
export default store;
store/state.js:
const state = {
showBack : false
}
export default state;
store/mutations.js:
const mutations = {
SET_SHOWBACK:(state,showBack)=>{
state.showBack = showBack;
}
}
export default mutations;
store/actions.js:
const actions = {
setShowBack({commit},showBack){
commit('SET_SHOWBACK',showBack)
}
}
export default actions;
main.js: