【发布时间】:2020-05-09 21:06:56
【问题描述】:
为了解释这个问题,请使用以下文档中的代码:
<template>
<div>
<b-form-tags v-model="value" no-outer-focus class="mb-2">
<template v-slot="{ tags, inputAttrs, inputHandlers, addTag, removeTag }">
<b-input-group aria-controls="my-custom-tags-list">
<input
v-bind="inputAttrs"
v-on="inputHandlers"
placeholder="New tag - Press enter to add"
class="form-control">
<b-input-group-append>
<b-button @click="addTag()" variant="primary">Add</b-button>
</b-input-group-append>
</b-input-group>
<ul
id="my-custom-tags-list"
class="list-unstyled d-inline-flex flex-wrap mb-0"
aria-live="polite"
aria-atomic="false"
aria-relevant="additions removals"
>
<!-- Always use the tag value as the :key, not the index! -->
<!-- Otherwise screen readers will not read the tag
additions and removals correctly -->
<b-card
v-for="tag in tags"
:key="tag"
:id="`my-custom-tags-tag_${tag.replace(/\s/g, '_')}_`"
tag="li"
class="mt-1 mr-1"
body-class="py-1 pr-2 text-nowrap"
>
<strong>{{ tag }}</strong>
<b-button
@click="removeTag(tag)"
variant="link"
size="sm"
:aria-controls="`my-custom-tags-tag_${tag.replace(/\s/g, '_')}_`"
>remove</b-button>
</b-card>
</ul>
</template>
</b-form-tags>
</div>
</template>
<script>
export default {
data() {
return {
value: ['apple', 'orange', 'banana', 'pear', 'peach']
}
}
}
</script>
但是当我将值作为对象数组而不是短字符串时,如何将对象的属性呈现为标记元素?
例如:如果我有值:[{name: 'apple', color: 'red'}, {name:'mango', color: 'yellow'}] 那么如何获得与上面相同的输出?
我尝试了类似<strong>{{ tag.name }}</strong> 的东西,但它不起作用,只给了我要删除的空标签,如下所示:
关于如何实现我想要在这里做的任何想法?
【问题讨论】:
-
用户如何在文本输入字段中输入标签作为对象?
标签: javascript vue.js vue-component bootstrap-vue