无法通过我的方法访问数据
Can't access to data from my method
我一直在尝试通过我的方法“itemChange”访问我的“selected”数据,但一点运气都没有。
我得到的错误是“this.selected 未定义”
我的问题是如何从我的方法访问我的组件数据?
我的组件实现
import VueSelect from 'vue-select';
Vue.component('v-select', VueSelect);
Vue.component('club-type',{
template: "#club-type",
props: ['options'],
data: {
selected: {title: 'My title', logo: 'logo here', info: 'info here'}
},
methods: {
itemChange: function () {
console.log("msg: " + this.selected['title']);
}
}
});
我的模板
<script type="text/x-template" id="club-type">
<v-select :options="options" label="title" :on-change="itemChange" :searchable="false" v-model="selected">
<template slot="option" scope="option">
<div class="float-left-items">
<div class="club-type-thumb" v-if="option.logo"><img v-bind:src="option.logo"></div>
<div class="club-type-title">{{ option.title }} <br/> <span class="pill club-type-pill"> AGE {{ option.age }}</span></div>
</div>
</template>
</v-select>
</script>
在我上面的模板中,如果我添加 v-model="selected" 它会显示错误“selected is undefined.
首先,您需要return您的数据:
data () {
return {
selected: { title: 'My title', logo: 'logo here', info: 'info here' }
}
},
那么,它不是一个数组,所以你不能像你在你的方法中尝试的那样访问它。您必须将其称为 属性.
this.selected.title
编辑:如@Phil 所述,第二点不是必需的。
我一直在尝试通过我的方法“itemChange”访问我的“selected”数据,但一点运气都没有。
我得到的错误是“this.selected 未定义”
我的问题是如何从我的方法访问我的组件数据?
我的组件实现
import VueSelect from 'vue-select';
Vue.component('v-select', VueSelect);
Vue.component('club-type',{
template: "#club-type",
props: ['options'],
data: {
selected: {title: 'My title', logo: 'logo here', info: 'info here'}
},
methods: {
itemChange: function () {
console.log("msg: " + this.selected['title']);
}
}
});
我的模板
<script type="text/x-template" id="club-type">
<v-select :options="options" label="title" :on-change="itemChange" :searchable="false" v-model="selected">
<template slot="option" scope="option">
<div class="float-left-items">
<div class="club-type-thumb" v-if="option.logo"><img v-bind:src="option.logo"></div>
<div class="club-type-title">{{ option.title }} <br/> <span class="pill club-type-pill"> AGE {{ option.age }}</span></div>
</div>
</template>
</v-select>
</script>
在我上面的模板中,如果我添加 v-model="selected" 它会显示错误“selected is undefined.
首先,您需要return您的数据:
data () {
return {
selected: { title: 'My title', logo: 'logo here', info: 'info here' }
}
},
那么,它不是一个数组,所以你不能像你在你的方法中尝试的那样访问它。您必须将其称为 属性.
this.selected.title
编辑:如@Phil 所述,第二点不是必需的。