【问题标题】:Laravel 5.7 - VUE Component - Property or method "continent_selected" is not defined on the instance but referenced during renderLaravel 5.7 - VUE 组件 - 属性或方法“continent_selected”未在实例上定义,但在渲染期间引用
【发布时间】:2019-07-20 00:18:01
【问题描述】:

我在 Laravel 5.7 中开发的 Vue 组件中遇到此错误。
你能告诉我我错过了什么吗?

只是continent_selectedcountry_selected的Input Binding不起作用,其余代码都可以。

属性或方法“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>

【问题讨论】:

    标签: laravel vuejs2 vue-component


    【解决方案1】:

    在您的data() 中,您有continents_selected 而不是continent_selected。删除大陆后的S,它应该可以工作。

    您的 Vue 试图使用一个不存在的变量(因为 s),这就是发生此错误的原因。

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2021-05-04
      • 2019-03-30
      • 2022-01-04
      • 2019-03-27
      • 2019-02-20
      • 2021-11-27
      相关资源
      最近更新 更多