gpt4 book ai didi

javascript - 使用 Vue JS 从计算属性中获取选择选项

转载 作者:行者123 更新时间:2023-12-03 05:45:03 25 4
gpt4 key购买 nike

我正在尝试使用计算属性来选择与 Vue 一起使用。

这是我正在使用的 fiddle :https://jsfiddle.net/2m18ab1v/13/

这是我的 View 模型的样子:

var vm = new Vue({
el: "#app-container",
data: {
offer: {
unit: '',
tags: '',
},
protocol: protocol
},
computed: {
getUnits: function(){
var unitsList = [];
var tagList = this.offer.tags.split(",");
console.log(tagList);
for (var i=0; i<tagList.length; i++){
unitsList += this.protocol[tagList[i]]["units"];
}
console.log(unitsList);
return unitsList;
}
}

});

以及数据中引用的“协议(protocol)”对象:

var protocol = {"wireless":
{
"units": {"bandwidth": "bit/s", "distance": "kilometers"},
"children": [],
}

我希望 <select> 选择“位/秒”和“公里”。我一直在我的模板中尝试这样的事情:

           <div id="app-container">
<select v-model="offer.unit">
<option v-for="item in getUnits" v-bind:value="value">
{{ item }}

</option>
</select>
</div>

console.log(unitsList) 返回 Object { bandwidth: Getter, distance: Getter, 1 more… }

显然,我想要得到的是某种对象。我对 Vue 有点陌生,我正在尝试理解“Getter”的工作原理。有没有直接的方法来获取它的值?

最佳答案

当你尝试收集单位时,在 js 中使用 += 不会得到你想要的结果。在此working fiddle ,我使用 concat 来代替,将 wireless 下的 units 与其他单元连接起来。 (假设您在与无线相同的数据树级别上有其他协议(protocol)类别)根据您的实际需要,您可能需要push。看看他们的文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

关于javascript - 使用 Vue JS 从计算属性中获取选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40362114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com