属性或方法“名称”未在实例上定义,但在Vue.js渲染期间引用了?

 收藏

我正在按照Udemy的课程学习VueJS,并且我有以下代码:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
        integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://kit.fontawesome.com/968b5b8bf4.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="app">
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-6">
                    <div class="card">
                        <div class="card-header">Nuevo Usuario</div>
                        <div class="card-body">
                            <form>
                                <div class="form-group">
                                    <label for="name" >Nombre Completo</label>
                                    <input v-model="name" type="text" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="email">Correo Electrónico</label>
                                    <input v-model="email" type="email" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="password">Contraseña</label>
                                    <input v-model="password" type="password" class="form-control" />
                                </div>
                                <button @click.prevent="addUser" class="btn btn-primary btn-block">Ingresar Usuario</button>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12 col-md-6">
                    <div class="card">
                        <div class="card-header">Actualizar Usuario</div>
                        <div class="card-body">
                            <form>
                                <div class="form-group">
                                    <label for="name">Nombre Completo</label>
                                    <input type="text" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="email">Correo Electrónico</label>
                                    <inputt type="email" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="password">Contraseña</label>
                                    <input type="password" class="form-control" />
                                </div>
                                <button @click.prevent="modifyUser" class="btn btn-primary btn-block">Guardar Cambios</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12 mt-3">
                    <table class="table table-bordered table-striped">
                        <thead class="text-center">
                            <th>#</th>
                            <th>Nombre Completo</th>
                            <th>Correo Electrónico</th>
                            <th>Acciones</th>
                        </thead>
                        <tbody>
                            <tr class="text-center" v-for="(user, index) in users">
                                <td>{{ index + 1 }}</td>
                                <td>{{ user.name }}</td>
                                <td>{{ user.email }}</td>
                                <td>
                                    <button class="btn btn-success btn-sm">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <button class="btn btn-danger btn-sm">
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <script src="main.js"></script>
</body>

</html>

然后,我的VueJS代码:

new Vue({
    el: '#app',
    data: {
        newUser: {
            name: '',
            email: '',
            password: '',
        },
        users: [],
    },
    methods: {
        addUser () {
            var nuevo = {}
            nuevo.name = this.newUser.name
            nuevo.email = this.newUser.email
            nuevo.password = this.newUser.password
            this.users.push(nuevo)
            this.newUser.name = ''
            this.newUser.email = ''
            this.newUser.password = ''
        },
    },
})

如您所见,我正在创建一个变量来保存表单获取的数据并将其发送到数组。然后使用v-for对其进行迭代,并将其显示在表格中。但我收到此错误:

vue.js:634 [Vue警告]:属性或方法“名称”未在   实例,但在渲染过程中被引用。确保此属性是   在data选项中或对于基于类的组件,通过   初始化属性。
回复
  • Tom 回复

     <input v-model="name" type="text" class="form-control" />
    

    应该

     <input v-model="newUser.name" type="text" class="form-control" />
    

    as you defined the data as {newUser: {name: ""}}.

    do same to your other fields such as email