【发布时间】:2020-07-14 15:23:05
【问题描述】:
当我在编辑页面时,我希望默认选择我保存的产品标签。
目前它只选择其中的 1 个并重复我有多少标签(示例)
代码
HTML
<el-col :span="22">
<el-select
v-model="form.tags"
multiple
filterable
style="width:100%;"
allow-create
default-first-option
placeholder="Choose tags for your product">
<el-option
v-for="item in tagss"
:key="item.id"
:label="item.name"
:value="item.name">
</el-option>
</el-select>
</el-col>
<el-form-item label="Categories">
<el-col :span="24">
<el-cascader
v-model="form.categories"
style="width: 100%;"
:options="cats"
:props="{
multiple: true,
checkStrictly: true,
value: 'id',
label: 'name',
}"
clearable
filterable>
</el-cascader>
</el-col>
</el-form-item>
Script
data() {
return {
cats: [],
tagss: [],
form: {
tags: [],
categories: [],
_method: 'PUT',
},
}
},
mounted () {
this.fetchProduct()
this.getData()
},
methods: {
getData () {
axios.get('/api/admin/products', {
headers: {
Authorization: 'Bearer ' + localStorage.getItem('access_token')
}
})
.then(response => {
this.brands = response.data.brands;
this.cats = response.data.categories;
this.tagss = response.data.tags;
})
.catch(function (error) {
console.log('error', error);
});
},
fetchProduct() {
axios
.get('/api/admin/products/'+this.$route.params.id, {
headers: {
Authorization: 'Bearer ' + localStorage.getItem('access_token')
}
})
.then(response => {
this.form.tags = response.data.data.tags
this.form.categories = response.data.data.categories
console.log('all tags from backend: ', this.tagss) // sample data below
console.log('this product tags from backend: ', response.data.data.tags) // sample data below
})
.catch(function (error) {
console.log('error', error);
});
}
}
这是上面代码中我的
console.log的示例结果
注意:我在这个问题中提到了我的标签,但也分享了我的类别代码,因为你可以看到我的类别有同样的问题。
知道可能是什么问题吗?
【问题讨论】:
-
我这辈子从来没有用过这个组件,但是我很好奇,我去查看他们的文档。我没有找到任何与您一样使用的组件示例,而是将该选项作为道具传递:vue-select.org/guide/values.html#tagging
-
@Raffobaffo docs
-
是的,这里它使用了不同的组件
el-cascader你正在使用el-select -
糟糕的是我正在做别的事情:D
-
大声笑,发生了!很好,我在你发给我链接后注意到了!
标签: typescript vue.js vuejs2