1
我有一个Vue组件,我使用internalValue
来访问value属性。我如何扩展这个也得到ID
?在组件中扩展Vue函数以显示ID
即internalValue = value, id
我都试过,但我不知道如何添加这个功能internalValue里面。我甚至试图通过将所有值的实例更改为id来获取ID,但它仍然吐出值。
我很乐意让他们成为一个即value, id
或访问他们像data.value
和data.id
初始化Vue公司
new Vue({
el: '#topic',
data: {
selectedTopic: null
}
});
使用组件
<div class="form-group" id="topic">
<topic v-model="selectedTopic"></topic>
</div>
注册组件
Vue.component('topic', require('./components/Topicselect.vue'));
组件
<template>
<div>
<label v-for="topic in topics" class="radio-inline radio-thumbnail">
<input type="radio" v-model="internalValue" name="topics_radio" :id="topic.id" :value="topic.name">
<span class="white-color lg-text font-regular text-center text-capitalize">{{ topic.name }}</span>
</label>
<ul class="hidden">
<li>{{ internalValue }}</li>
</ul>
</div>
</template>
<script>
export default {
props: ['value'],
data() {
return {
internalValue: this.value,
topics: []
}
},
mounted(){
axios.get('/vuetopics').then(response => this.topics = response.data);
},
watch: {
internalValue(v){
this.$emit('input', v);
console.log('Topicselect: the value is ' + this.internalValue);
}
}
}
</script>
谢谢你,这是魔术在那里:) –
@ClintonGreen不客气:)我在改变后的模板编辑,特别是':检查=“值&& topic.id == value.id”'在某些时候你开始使用一个值而不是null。 – Bert
呀一日2次,我得到了ID =零错误,但我看到的编辑。感谢堆。 –