原来的问题是s中没有找到el-select
s 的初始值(via ) ,所以显示了未知值。鉴于OP关于不同值类型的答案以及显示的 ,原因很可能是初始化为(a ),而这些值都是s:v-model="selectTagPicker"
option
el-select
el-select
0
selectTagPicker
"0"
String
option
Number
<template>
<el-select v-model="selectTagPicker">
<el-option
v-for="(tag,tagIndex) in allTags"
:key="tag.id"
:value="tagIndex" Number
:label="tag.name"
>
{{tag.name}}
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
allTags: [...],
selectTagPicker: "0" String (❌does not match any values)
}
}
}
</script>
原始问题的演示
将option
值转换为String
(通过:value="String(tagIndex)"
模板)解决了这个问题,因为第一个项目将被分配"0"
,匹配初始值selectTagPicker
:
<template>
<el-select v-model="selectTagPicker">
<el-option
v-for="(tag,tagIndex) in allTags"
:key="tag.id"
:value="String(tagIndex)" String (✅first value matches `selectTagPicker`)
:label="tag.name"
>
{{tag.name}}
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
allTags: [...],
selectTagPicker: "0" String
}
}
}
</script>
OP 解决方案的演示
或者,OP 可以将初始值更改selectTagPicker
为0
(a Number
):
<template>
<el-select v-model="selectTagPicker">
<el-option
v-for="(tag,tagIndex) in allTags"
:key="tag.id"
:value="tagIndex" Number
:label="tag.name"
>
{{tag.name}}
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
allTags: [...],
selectTagPicker: 0 Number (✅matches first value)
}
}
}
</script>
替代解决方案的演示 1
id
另一种解决方案是使用选项项目中的现有道具(例如, )作为option
的值,然后初始化selectTagPicker
为该道具的值:
<template>
<el-select v-model="selectTagPicker">
<el-option
v-for="tag in allTags"
:key="tag.id"
:value="tag.id"
:label="tag.name"
>
{{tag.name}}
</el-option>
</el-select>
</template>
<script>
export default {
data() {
const allTags = [...]
return {
allTags,
selectTagPicker: allTags[0].id
}
}
}
</script>
替代解决方案 2 的演示