Vuex |地图状态-页面刷新之前beforeMount()的作用域问题

时间:2019-08-22 09:43:45

标签: vue.js vuejs2 vuex

我在Vuex mapState的范围界定问题。我需要在页面上本地输出用户名,而不能由v-model在我的输入字段中对它进行反应式更新(对于同一用户名数据)。

以下代码可以正常工作,但是,如果用户刷新页面,状态将丢失并返回未定义状态。

computed: {
  ...mapState(["userProfile"]),
},

beforeMount() {
  this.localUserProfile.name = this.userProfile.name;
  console.log("localUserProfile: " + this.localUserProfile.name); 
},

1 个答案:

答案 0 :(得分:0)

我认为可能发生的情况是,仅当您直接刷新页面时在挂载组件之后才设置Vuex中的<composite-id name="id" class="it.hr.model.JobHistoryId"> <key-property name="employeeId" type="int"> <column name="EMPLOYEE_ID" precision="6" scale="0" /> </key-property> <key-property name="startDate" type="date"> <column name="START_DATE" length="7" /> </key-property> </composite-id> 数据吗?在这种情况下,userProfile在分配时可能仍未定义。如果发生这种情况,可以在该组件中添加一个观察程序,以检查userProfile.name中的更改并更新userProfile(如果在安装期间未设置)。

也:您是否看过v-once指令? https://vuejs.org/v2/api/#v-once 这样可以防止在值更改时更新用户名,而不必引用本地副本。