javascript - Vue级联自动完成对象键
问题描述
我尝试在 vue js 中进行 3 或 4 级级联自动完成。但它不能正常工作。这是 orjinal codepen
<div id="app">
<v-app id="inspire">
<v-card>
<v-card-text>
<v-layout wrap>
<v-flex xs3 md3>
<v-autocomplete v-model="category" :items="categories" label="Category" full-width solo hint="Random set of categories">
</v-autocomplete>
</v-flex>
<v-flex xs3 md3>
<v-autocomplete v-model="purpose" :items="purposes" label="Purpose" full-width solo hint="Based on the selected category">
</v-autocomplete>
</v-flex>
</v-layout>
</v-card-text>
</v-card>
</v-app>
</div>
js
new Vue({
el: "#app",
data() {
return {
model: null,
product: null,
category: null,
purpose: null,
categoriesPurposes: {
"Farm Animals": ["cow", "sheep", "hen"],
Directions: ["left", "right", "up", "down"],
Time: ["past", "present", "future"],
Element: ["air", "water", "tierra", "fire"]
}
};
},
computed: {
categories() {
return Object.keys(this.categoriesPurposes);
},
purposes() {
if (!this.category) {
return null;
} else {
return this.categoriesPurposes[this.category];
}
}
}
});
我尝试了 1 个更深的级别,但第二个自动完成看起来对象。
我更改了数据,例如:
categoriesPurposes: {
Asia:{
"Farm Animals": ["cow", "sheep", "hen"],
Directions: ["left", "right", "up", "down"],
Time: ["past", "present", "future"],
Element: ["air", "water", "tierra", "fire"]
}
}
但这次第二个自动完成数据似乎是对象
我如何访问密钥并显示它们?谢谢。
解决方案
你给了一个对象,它是 Vuetify 定义的非法值
[Vue warn]: Invalid prop: type check failed for prop 'items'. Expected Array, got Object
问题是你返回this.categoriesPurposes[this.category]
的是一个对象而不是对象数组。为了能够传递对象数组,您需要查看 Vuetify 的规范。
这应该工作
categoriesPurposes: {
"Farm Animals": [
{
text: "This is the cow",
value: "cow",
},
{
text: "This is the sheep",
value: "sheep",
},
{
text: "This is the hen",
value: "hen"
}
],
Directions: ["left", "right", "up", "down"],
Time: ["past", "present", "future"],
Element: ["air", "water", "tierra", "fire"]
}
在这里做了一个工作示例
推荐阅读
- android - 如何使用android中的数据绑定在布局编辑器中隐藏视图
- html - 使用带有自定义 CSS 的 type="search" 时,'x' 未显示在 Firefox 输入中
- java - Drools:为什么一个撤回的事实仍然出现在收集中
- sql-server - SSRS 报告未显示来自数据库的图像
- javascript - 如何用 JS 将 HTML 重写为 HTML?
- db2 - 如何在 DB2 9.5 和 11.5 中将 CLOB 强制转换为 INTEGER?
- delphi - 将字符串转换为 Cardinal / UInt32
- python - 您如何查询文本字段内的对象,以对其进行处理?
- android - 以编程方式启用/禁用“enabled_notification_listeners”?
- javascript - 无法使用已售罄消息禁用缺货产品变体的功能