尝试在选择模型中绑定JSON数据时,在Vue JS中出现typerror

时间:2018-08-26 05:28:48

标签: laravel vue.js

我想将select选项与返回的JSON数据绑定。但是,当我执行API调用并将选项模型组设置为返回的JSON时,出现错误“ TypeError:无法设置未定义的属性'groups'”。

这是vue文件

<template>
    <div class="register">
        <div class="container">
            <div class="row">
                <div class="col">
                    <h3 class="mb-10">Register as a volunteer</h3>
                    <form action="">
                        <div class="form-group">
                            <label for="first_name">First Name</label>
                            <input type="text" v-model="first_name" placeholder="First Name" class="form-control" id="first_name">
                        </div>
                        <div class="form-group">
                            <label for="last_name">Last Name</label>
                            <input type="text" v-model="last_name" placeholder="Last Name" class="form-control" id="last_name">
                        </div>
                        <div class="form-group">
                            <label for="group">Select Institution/Organization/Company</label>
                            <select v-model="group" class="form-control" id="group">
                                <option v-for="group in groups" v-bind:name="name">{{ group.code }}</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="exampleInputEmail1">Email address</label>
                            <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
                            <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                        </div>
                        <div class="form-group">
                            <label for="phone">Phone</label>
                            <input type="text" v-model="adv_phone" placeholder="Phone" class="form-control" id="phone">
                        </div>
                        <div class="form-group">
                            <label for="password">Password</label>
                            <input type="password" v-model="adv_password" class="form-control" id="password" placeholder="Password">
                        </div>
                        <div class="form-group">
                            <label for="confirm-password">Confirm Password</label>
                            <input type="password" v-model="adv_password" class="form-control" id="confirm-password" placeholder="Confirm Password">
                        </div>
                        <div class="from-group">
                            <input type="submit" class="btn btn-primary" value="Register">
                            &nbsp;
                            <router-link :to="{name:'home'}">
                            <a class="btn btn-outline-secondary">Cancel</a>
                            </router-link>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="register-form"></div>
    </div>
</template>

<script>
    export default {
        mounted(){
            this.getGroups()
        },
        data (){
            return {
                group: '',
                groups:'',
                first_name:'',
                last_name:'',
                adv_email:'',
                adv_phone:'',
                adv_password:'',
                confirm_password:''
            }
        },
        methods:{
            getGroups(){
                axios.get('/api/group/get/all')
                    .then(function (response) {
                        console.log(response);
                        this.groups = response.data;
                        //console.log(groups);
                    })
                    .catch(function (error) {
                        console.log(error);
                    })
                    .then(function () {
                        // always executed
                    });
            }
        }

    }
</script>

这是我的json返回

[{"id":8,"name":"Villa Maria Academy","code":"Vil-9458-3786","advisors":25,"students":99,"subscription_time":99,"subscription_type":0,"admin_name":"","admin_email":"","phone":"817879234","address":"Abcde street","state":"Pennsylvania","zip":16502,"apt":"","city":"Erie"},{"id":9,"name":"Cathedral Prep","code":"Cat-1959-1432","advisors":99,"students":99,"subscription_time":99,"subscription_type":0,"admin_name":"","admin_email":"","phone":"0","address":"","state":"","zip":0,"apt":"","city":""}]

设置this.groups = response.data时,为什么会出现类型错误?

1 个答案:

答案 0 :(得分:0)

您可以使用箭头功能解决问题。

        methods:{
        getGroups(){
            axios.get('/api/group/get/all')
                .then(response => {
                    console.log(response);
                    this.groups = response.data;
                    //console.log(groups);
                })
                .catch(error => {
                    console.log(error);
                })
                .then(() => {
                    // always executed
                });
        }
    }