Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

vue状态缓存问题

这是写的一个tab切换,点击的时候,通过父传子的storyList来改变内容,点击的时候跳转到详情页,返回详情页的时候怎么返回之前选择状态展示对应的内容

<ul class="hw_ul">
  <li v-for="(item,index) in hwList" :key="index"
      :class="index == rangeCode ? 'act' : ''"
      @click="rangeClick(index)">{{item}}
  </li>
</ul>
<keep-alive>
  <home-content :data="storyList" :thatCode="rangeCode"></home-content>
</keep-alive>



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
// 父组件
computed: {
  rangeCode: {
    get () {
      return this.$store.state.rangeCode
    },
    set (val) { return val }
  }
},
methods: {
  rangeClick (idx) {
    this.$store.commit('setRangeCode', idx)
  }
}

// vuex
const store = new Vuex.Store({
  state: {
    rangeCode: 0
  },
  mutations: {
    setRangeCode (state, code) {
      state.rangeCode = code
    }
  }
})

export default store

// main.js
import store from '@/state/index' // 根据自己的路径引入
Vue.prototype.$store = store

使用vuex保存状态


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...