vue.js - Vue:计算属性的初始触发
问题描述
在我的 Vue 组件中,我有一个计算属性,它监视状态和组件,并返回一个对象。
currentOrganization () {
if (this.$store.state.organizations && this.selectedOrganization) {
return this.$store.state.organizations.filter(org => org.id === this.selectedOrganization)[0];
}
}
不幸的是,这个属性在组件加载时不会自动更新,尽管我可以看到我同时拥有this.$store.state.organizations
和this.selectedOrganization
。但是,当我通过下面的选择组件向我的 Vuex 存储发送操作时,它会更新:
<b-form-select id="orgSelect"
v-model="selectedOrganization"
:options="this.$store.state.organizations.map(org => {return {value: org.id, text:org.name}})"
size="sm"
v-on:input="currentOrganizationChange()"
class="w-75 mb-3">
<template slot="first">
<option :value="null" disabled>Change Organization</option>
</template>
</b-form-select>
该组件调度一个 Vuex 动作:
currentOrganizationChange () {
this.$store.dispatch('setCurrentOrganization', this.selectedOrganization);
},
我如何currentOrganization()
最初进行计算?
解决方案
在您使用它之前,计算不会计算。
console.log
它,或者只是写当前的组织;在执行的代码中的某处,它将计算:)
推荐阅读
- sql-server - 从 T-SQL 中的 json 对象获取第一个值或默认值
- python - 打印没有值的局部变量的名称
- r - 使用 rmarkdown 时保存环境出错
- c# - 是否可以区分 WPF DataGrid 上的 PreviewMouseLeftButtonDown 事件,无论它是直接来自网格还是单元格项目?
- vim - 在 VIM 中,如何避免在执行长命令后出现 hit-enter 提示?
- android - 如何创建/更新会话?(MasterCard Gateway 集成在 android 和 iOS 中)
- python - 在调用python中的bsub函数时将参数传递给bash脚本
- python - lambda 函数中的“_”是什么意思,为什么要使用它?
- html - 如何在导航栏中的图像下放置标签?
- javascript - 将一个空元素推入 javascript 数组