简体   繁体   English

Vue.js 与 VueX - Vue 实例生命周期之间的内容闪烁

[英]Vue.js with VueX - content flash between Vue instance lifecycle

I have a main page.我有一个主页。 When user logs in, it will detect user's web application version.当用户登录时,它会检测用户的 Web 应用程序版本。 If update needed, it shows an update instruction modal.如果需要更新,它会显示一个更新指令模式。 Currently, due to some issues, we like to change the update instruction depends on a specific version.目前,由于一些问题,我们喜欢根据特定版本更改更新说明。

For example:例如:

In the normal situation, the update instruction shows like please update , but once detect the specific version, it will change to please contact us正常情况下,更新说明显示为please update ,但一旦检测到具体版本,就会更改为please contact us

The issue here is that when user logs in, if user's version is specific version, the update instruction will flash from please update to please contact us .这里的问题是,当用户登录时,如果用户的版本是特定版本,更新说明会从请更新请联系我们闪烁。

I tried to use the lifecycle, beforeCreated or beforeMounted to detect the version then set the instruction, but the flash still.我尝试使用生命周期, beforeCreatedbeforeMounted来检测版本然后设置指令,但闪存仍然存在。

I think the reason caused the issue is when main page mounted, the modal mounted too.我认为导致问题的原因是安装主页时,也安装了模态。 Because at that moment, the IsSpecificVersion in VueX is false , so it shows please update , then state chage, it becomes please contact us因为那个时候VueX中的IsSpecificVersion是false ,所以显示please update ,然后state chage,变成了please contact us

The result I expect as below我期望的结果如下

user login==>detect==>modal only shows the content depends on the detected version

How can I set the value properly in Vue's lifecycle?如何在 Vue 的生命周期中正确设置值?

Main page主页

<template>
  <div class="welcome-page">
    <update-modal></update-modal>
</template>
<script>
import UpdateModal from './UpdateModal.vue'
export default {
  components: {
    UpdateModal,
 },
async beforeCreate() {
  ...
  set IsSpecificVersion to true in VueX
}
</script>

Update instruction modal更新指令模态

<template>
 <b-modal
   v-model="show"
 >
 <p v-if="IsSpecificVersion">please update</p>
 <p v-else>please contact us</p>
</template>
<script>
 computed: {
   show: {
    get: function () {
      return this.$store.state.isShow
    },
    set: function() {
      xxxxxx
    }
  },
  IsSpecificVersion() {
    return this.$store.state.IsSpecificVersion
  }
 }
</script>

It loooks like you are using css styles to control the display and hide of the modal.看起来您正在使用 css 样式来控制模式的显示和隐藏。 If this is the case, the modal will always exist on the page, and the state of vuex not have changed when you show it.如果是这种情况,modal 将一直存在于页面上,并且显示时 vuex 的状态并没有改变。

In my use, first change the state and then show the modal and it will not flash.在我的使用中,先改变状态,然后显示模态,它不会闪烁。

Perhaps you can check the order of instructions in the program to make sure that the status has changed when displayed.也许您可以检查程序中的指令顺序,以确保显示时状态已更改。

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

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