简体   繁体   English

Vue.js Vuex状态更改不会使用v-if重新呈现模板

[英]Vue.js Vuex State Change doesn't rerender a template with v-if

I've tried having the v-if value set in computed, data, passed as props and just a direct reference to state but it NEVER re renders despite the fact that the state I check for is changed to true. 我试过在计算的数据中设置v-if值,将其作为prop传递,并且只是直接引用状态,但是即使我检查的状态已更改为true,也永远不会重新呈现。

Currently I have it directly referencing the store 目前,我直接参考商店

<template v-if="this.$store.state.showReviews">

I perform an AJAX Request in my Vuex Action 我在Vuex Action中执行AJAX请求

getBikeReviews(context, i){
  console.log("getBikeReviews");
  axios.get('http://mpaccione.com/wp-content/themes/webdev/projects/Web_Design_4/Creating_Online_Store/api/get_reviews.php?index='+i).then((res) => {
    context.commit('storeBikeReviews', {"index": i, "json": res.data});
    context.commit('showReviews', true);
  });
}

The State gets Mutated to true 国家变得真实

showReviews (state, payload){
  console.log("showReviews");
  state.showReviews = payload;
}

However the template never re-renders. 但是,模板永远不会重新渲染。

Have you tried using getters or computed values? 您是否尝试过使用吸气剂或计算值?

computed: {
  showReviews() {
    return this.$store.state.reviews || []
  },
  reviews() {
    return this.$store.state.showReviews === true
  }
}

also, it's a good idea to use Vue.$set whenever possible instead of simple assignment. 另外,最好在可能的情况下使用Vue。$ set代替简单的分配。

Import Vue from 'vue';

showReviews (state, payload){
  Vue.$set(state, 'showReviews', payload);
}

while this may not resolve the issue with some value types, IMHO, it's a good idea to just apply all the time, so you don't forget the one that does need it. 尽管使用某些值类型(恕我直言)可能无法解决问题,但始终应用是个好主意,因此您不要忘记需要它的对象。

finally, if you're really struggling to get the reactivity to kick in, you can force a re-render by calling this.$forceUpdate(); 最后,如果您真的很想获得反应性,则可以通过调用this.$forceUpdate();来强制重新渲染this.$forceUpdate(); (or Vue.$forceUpdate(); ) after your mutation (或Vue.$forceUpdate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM