如何在 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>