我在Laravel 5.7中开发的Vue组件中遇到此错误。
你能告诉我我想念什么吗?
仅 continent_selected 和 country_selected 的输入绑定无效,其余代码很好。
属性或方法“ continent_selected”未定义 在实例上,但在渲染期间被引用。确保>该属性在数据中都是反应性的 选项,或者对于基于类的组件,通过初始化 财产。
这是我的代码:
<template>
<div>
<div class="form-group continent_id">
<select name="continent_id" v-model="continent_selected" id="continent_id" class="selectpicker" data-live-search="false" title="Pick a continent">
<option v-if="continents.length>0" v-for="continent in continents" v-bind:value="continent.id">
{{ continent.name }}
</option>
</select>
</div>
<div class="form-group country_id">
<select name="country_id" v-model="country_selected" id="country_id" class="selectpicker" data-live-search="true" title="Pick a country">
<option v-for="(country, index) in countries" v-bind:value="country.id" >
{{ country.name }}
</option>
</select>
</div>
<span>Continent Selected: {{ continent_selected }}</span>
<span>Country Selected: {{ country_selected }}</span>
</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.');
this.loadData();
},
data() {
return {
continents: [],
countries: [],
continents_selected: '',
country_selected: '',
}
},
methods: {
loadData: function() {
axios.get('/api/continents')
.then((response) => {
// handle success
this.continents = response.data.data;
this.getAllCountries(response.data.data);
})
.catch((error) => {
// handle error
console.log(error);
})
.then(() => {
// always executed
});
},
getAllCountries: function(continents) {
var j = 0;
for (var i = 0, len = continents.length; i < len; i++) {
for (var key in continents[i].active_countries) {
this.countries[j] = {id: continents[i].active_countries[key], name: key};
j++;
}
}
}
},
}
</script>
答案 0 :(得分:1)
在您的data()
中,您有continents_selected
而不是continent_selected
。除去大陆后的S,它应该可以工作。
您的Vue试图使用一个不存在的变量(由于s
),这就是发生此错误的原因。