使用VueJS的ComboBox模板组件
P粉550323338
P粉550323338 2023-09-03 22:34:50
[Vue.js讨论组]
<p>我想使用 <code>vuejs v3</code> 制作一个 <strong>Combobox</strong> 模板组件,为此我有以下代码:</p> <pre class="brush:html;toolbar:false;">&lt;template&gt; &lt;select name={{selector_name}} class= &quot;combobox&quot; id={{selector_id}}&gt; v-for=opt in {{selector_options}} &lt;option value=opt&gt; opt &lt;/option&gt; &lt;/select&gt; &lt;/template&gt; &lt;script&gt; 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: &quot;combobox&quot; }, selector_options: { type: Array, required: true } }, methods: { onChange: (event) =&gt; { console.log(event.target.value); }, }, computed: {}, } &lt;/script&gt; </pre> <p>但是我使用 <code>v-for</code> 的方式对我来说似乎不正确,你能告诉我如何纠正吗?提前致谢。</p>
P粉550323338
P粉550323338

全部回复(1)
P粉616111038

我看到很多东西,为了清楚地回答你的问题,v-for是用在元素上的。

<template>
    // For mor readability i recommend you do bind your property:
    //  - name={{selector_name}} become :name="selector_name"
    <select :name="selector_name" class= "combobox" :id="selector_id">
        <!-- v-for is required with a unique key, you can take the index and use it -->
        <option v-for="(opt, index) in selector_options" :key="`opt ${index}`" value=opt>
           {{ opt }}
        </option>
    </select>
</template>

您不能定义同名的 props 和 data。 Props,在数据中注入属性和值。 完全相同,但数据来自父级,您可以在父级中将值传递给子级。

因此,如果您需要传递数据,请使用 props,但不要在数据内部定义它。

有一个工作示例所有这些(我使用数据而不是道具来提高可读性)。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号