javascript - Vue-Form-Generator 模式对计算属性没有反应
问题描述
我的数据this.coinPairingOptions
中有计算属性,需要根据 this 中的其他一些字段来呈现其单选按钮schema
。我减少了文件中的代码量以节省空间。
data: function () {
return {
schema: {
{model: "symbolPair", type: "radios", label: "Pair with", values:
this.coinPairingOptions, required: true}
},
computed: {
coinPairingOptions() {
console.log("computing coinPairingOptions")
let coin = this.model.symbol.toUpperCase();
let options = [];
if (this.model.exchange === 'Coinbase') {
options = this.getCoinbasePairs
} else if (this.model.exchange === 'Binance') {
options = this.getBinancePairs
} else {
}
console.log(options.get(coin));
return options.get(coin);
},
}
在开发工具中,我可以看到计算属性更改为正确的值,但数据中没有更改。显然,这是适当的行为,但有什么办法解决这个问题?我尝试放入{{this.coinPairingOptions}}
html 并且它出错,因为它是一个最初没有值的计算属性。
任何帮助,将不胜感激!
解决方案
你不能使用computed property
in data
,因为在做data
之前评估computed properties
。
您可以使用 awatcher
来达到预期的结果。查看文档,您可以添加参数immediate
以使用表达式的当前值立即触发回调。
Computed properties
已经可以template
通过 using访问{{}}
。您无需将 athis
放在计算的前面。
推荐阅读
- shell - 使用 shell 脚本替换 JSON 文件中 char 的第一个实例
- oracle - Oracle:调用中的参数数量或类型错误
- php - 如何在网站前端按 SKU 搜索产品
- javascript - 如何用 jest 模拟和测试链式函数?
- bash - 为什么 bash 不在 $() 运算符中拆分字段?
- node.js - 是否可以从 PM2 获取流程元数据?
- c++ - 为什么在此代码中隐式转换为常量迭代器会失败?
- python - 无法在 add_url_rule 中设置方法
- c# - 如何在鼠标按下时同时旋转多个对象?
- windows - 是否有 PowerShell Get-Content 函数可以根据第一个字符提取行?