简体   繁体   English

Vuex状态更改不会使组件重新渲染?

[英]Vuex state change doesn't make component re-render?

I have a navbar which has conditional rendering depending on authentication: 我有一个导航栏,根据身份验证具有条件渲染:

<template>
  <div id="routing">
    <b-navbar variant="light" type="light">
      <b-navbar-brand>
        <img id="drage" src="../assets/icon.svg"/>
        <b-nav-text id="txt">
          drac1
        </b-nav-text>
        <div v-if="loggedIn === true">
          <router-link id="link" to="/" exact>
            Home
          </router-link>
          <router-link id="link" to="configuration" exact>
            Config
          </router-link>
          <b-nav-text id="hellotext">
            Hello, {{ username }}
          </b-nav-text>
        </div>
      </b-navbar-brand>
    </b-navbar>
  </div>
</template>

<script>
  export default {
    name: 'topnavbar',
    data () {
      console.log(this.$store.state.loggedIn) // Changes from false to true as it is supposed to, and logs it. 
      return {
        username: this.$store.state.username,
        loggedIn: this.$store.state.loggedIn
      }
    }
  }
</script>

THe value loggedIn which the conditional render depends upon does change from false to true when the user has logged in. However, the router-link items as well as the username nav text never show up. 当用户登录时,条件渲染依赖的值loggedIn会从false变为true 。但是, router-link项以及用户名nav文本永远不会显示。

Why doesn't a change in store state trigger a re-render of the navbar in this case? 在这种情况下,为什么存储状态的更改不会触发重新渲染导航栏?

You are using it wrong, in order for the changes to be reflected you need to use computed properties: 您使用它是错误的,为了反映更改您需要使用计算属性:

 export default {
    name: 'topnavbar',
    data () {

      return {

      }
    },

    computed: {
        username(){
            return  this.$store.state.username
        },
        loggedIn(){
            return  this.$store.state.loggedIn
        }
    }
  }

暂无
暂无

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

相关问题 在 vuex 中更改 state 后,Vue 组件的视图不会重新渲染? - Vue component's view doesn't re-render after state change in vuex? 使用 vuex getter 重新渲染组件 - Re-render a component using vuex getters Nuxt Vue.js:Vuex 全局 state 更改不会触发 v-for 循环中的重新渲染组件 - Nuxt Vue.js: Vuex global state change does not trigger re-render component in v-for loop 为什么在更改 Pinia state(删除 Array 中的一个 object)后不重新渲染 Vue 组件? - Why doesn't re-render Vue the component after changing the Pinia state (deleteing an object in Array)? VueJS 与 Vuex - 用拼接替换存储在 Vuex 中的数组中的项目(对象)不会重新呈现项目列表 - VueJS with Vuex - Replace item (object) in array stored in Vuex with splice doesn't re-render list of items Vuex商店更新未在组件中应用重新渲染 - Vuex store update not applying re-render in component Vuex 用 Deep change 重新渲染 v-for - Vuex Re-render of v-for with Deep change 使用 Lodash.remove 通过 Vuex 突变更改 state 不会触发我的 Vue 组件中的反应性重新渲染 - Changed state through Vuex mutation using Lodash.remove does not trigger a reactive re-render in my Vue component vuex 状态更改后,Vue 组件不会重新渲染 - Vue component doesn´t rerender after vuex state change 如何在其数据(和状态)完好无损的情况下重新渲染组件(Vue)? - How to re-render a component (Vue) with its data (and state) intact?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM