组件中扩展Vue函数显示ID
Extend Vue function in component to display ID
我有一个 Vue 组件,我正在使用 internalValue
访问 value 属性。我将如何扩展它以获得 ID
?
即内部值=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>
使用所选主题作为您的价值。基本上,完全消除 internalValue
,并在单击时发出与任何给定单选按钮关联的主题。这将满足 v-model
,因为它会监听 input
事件(除非您自定义它)。
export default {
props: ['value'],
data () {
return {
topics: []
}
},
methods:{
selectValue(topic){
this.$emit('input', topic)
}
},
mounted(){
axios.get('/vuetopics').then(response => this.topics = response.data);
}
})
还有你的模板
<template>
<div>
<label v-for="topic in topics" class="radio-inline radio-thumbnail">
<input type="radio" @click="selectValue(topic)" name="topics_radio" :id="topic.id" :value="topic.name" :checked="value && topic.id == value.id">
<span class="white-color lg-text font-regular text-center text-capitalize">{{ topic.name }}</span>
</label>
<ul class="hidden">
<li>{{ value }}</li>
</ul>
</div>
</template>
这会将你的 Vue 中的 selectedTopic
设置为一个主题,类似于
{
id: 2,
name: "some topic"
}
取决于您在模板中的使用方式。
我有一个 Vue 组件,我正在使用 internalValue
访问 value 属性。我将如何扩展它以获得 ID
?
即内部值=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>
使用所选主题作为您的价值。基本上,完全消除 internalValue
,并在单击时发出与任何给定单选按钮关联的主题。这将满足 v-model
,因为它会监听 input
事件(除非您自定义它)。
export default {
props: ['value'],
data () {
return {
topics: []
}
},
methods:{
selectValue(topic){
this.$emit('input', topic)
}
},
mounted(){
axios.get('/vuetopics').then(response => this.topics = response.data);
}
})
还有你的模板
<template>
<div>
<label v-for="topic in topics" class="radio-inline radio-thumbnail">
<input type="radio" @click="selectValue(topic)" name="topics_radio" :id="topic.id" :value="topic.name" :checked="value && topic.id == value.id">
<span class="white-color lg-text font-regular text-center text-capitalize">{{ topic.name }}</span>
</label>
<ul class="hidden">
<li>{{ value }}</li>
</ul>
</div>
</template>
这会将你的 Vue 中的 selectedTopic
设置为一个主题,类似于
{
id: 2,
name: "some topic"
}
取决于您在模板中的使用方式。