【发布时间】:2018-06-27 23:13:31
【问题描述】:
我正在尝试使用单选按钮创建一个自定义组件,但只能使用标签的值而不是值属性来获得工作方式。
例如,改为标签 1、2、3 => 显示标签“全部”、“私人”、“专业”并获取值 1、2、3
代码 sn-p 不工作,但 external link 到代码框工作。
Vue.component('radio-button', {
props: ['name', 'label', 'value'],
template: `
<label class="radio">
<input type="radio" :value="label" :name="name" v-model="radioButtonValue">
<span>{{ label }}</span>
</label>
`,
computed: {
radioButtonValue: {
get: function() {
return this.value
},
set: function() {
this.$emit("change", this.label)
}
}
});
Vue.component('example-form', {
template: `
<div class="row">
<div class="col-lg-1 col-centered">
Test for component with radio buttons
</div>
<div>
<radio-button name="options" label="1" :value="selectedAdvertiser" @change="changeAdvertiser"/>
<radio-button name="options" label="2" :value="selectedAdvertiser" @change="changeAdvertiser"/>
<radio-button name="options" label="3" :value="selectedAdvertiser" @change="changeAdvertiser"/>
<hr>
<div class="result">
Radio button selection: {{selectedAdvertiser}}
</div>
</div>
</div>
`,
data: function() {
return {
selectAdvertiser: "1"
}
},
methods: {
changeAdvertiser: function(newValue) {
this.selectedAdvertiser = newValue
}
}
});
var App = new Vue({
el: '#my-app"
template: `<example-form></example-form>`
});
<script src="https://unpkg.com/vue"></script>
<body>
<div id="my-app"></div>
</body>
【问题讨论】:
标签: javascript binding vuejs2 vue-component