<p>我想使用 <code>vuejs v3</code> 制作一个 <strong>Combobox</strong> 模板组件,为此我有以下代码:</p>
<pre class="brush:html;toolbar:false;"><template>
<select name={{selector_name}} class= "combobox" id={{selector_id}}>
v-for=opt in {{selector_options}}
<option value=opt>
opt
</option>
</select>
</template>
<script>
export default {
name: 'ComboBox',
data() {
return {
selector_name: null,
selector_id: null,
selector_options : null,
}
},
props: {
selector_name: {
type: String,
required: true
},
selector_id: {
type: String,
default: "combobox"
},
selector_options: {
type: Array,
required: true
}
},
methods: {
onChange: (event) => {
console.log(event.target.value);
},
},
computed: {},
}
</script>
</pre>
<p>但是我使用 <code>v-for</code> 的方式对我来说似乎不正确,你能告诉我如何纠正吗?提前致谢。</p>
我看到很多东西,为了清楚地回答你的问题,v-for是用在元素上的。
您不能定义同名的 props 和 data。 Props,在数据中注入属性和值。 完全相同,但数据来自父级,您可以在父级中将值传递给子级。
因此,如果您需要传递数据,请使用 props,但不要在数据内部定义它。
有一个工作示例所有这些(我使用数据而不是道具来提高可读性)。