【发布时间】:2016-12-21 23:31:30
【问题描述】:
如何在 vue.js 中选择选项。 React 有类似this.props.children 的东西,我用它来处理这种情况。如何在 vue.js 中解决这个问题?
我也尝试使用 $children 访问孩子,但它始终是一个空数组。
在此示例中,选择框没有选项。 在 jsFiddle 上也添加了示例:https://jsfiddle.net/kt8urx7d/
var FormComp = Vue.extend({
template: '#form-comp'
});
Vue.component('form-comp', FormComp);
var SelectField = Vue.extend({
template: '#select-field'
});
Vue.component('select-field', SelectField);
var SelectFieldOption = Vue.extend({
template: '#select-field-option'
});
Vue.component('select-field-option', SelectFieldOption);
new Vue({
el: '#container',
template: '<form-comp></form-comp>'
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<script type="x/template" id="form-comp">
<form>
<h1>This is my Form</h1>
<select-field name="my-select-field">
<select-field-option value="my-value-1">My Text 1</select-field-option>
<select-field-option value="my-value-2">My Text 2</select-field-option>
</select-field>
</form>
</script>
<script type="x/template" id="select-field">
<select name="{{ name }}">
<!-- include children: select-field-option -->
</select>
</script>
<script type="x/template" id="select-field-option">
<option value="{{ value }}">{{ content }}</option>
</script>
<div id="container"></div>
【问题讨论】:
-
能否请您附上脚本?
-
我已经添加了脚本
-
嗯,这仍然不完整,我们缺少组件的模板。我最好的猜测是模板是错误的。例如,FormComp 组件的标签在哪里(例如
<form-comp>)。你读过文档吗?请尝试在jsfiddle上写下您的想法,我们或许可以为您提供更好的帮助。 -
我创建了以下 jsfiddle:jsfiddle.net/kt8urx7d 如何将选项放入选择框?
标签: javascript vue.js vue-component