【问题标题】:Property or method "name" is not defined on the instance but referenced during render in vuejs?属性或方法“名称”未在实例上定义,但在 vuejs 渲染期间引用?
【发布时间】:2020-07-07 20:09:06
【问题描述】:

我正在通过 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 警告]:属性或方法“名称”未在 实例,但在渲染期间引用。确保该属性是 反应式,无论是在数据选项中,还是对于基于类的组件,通过 初始化属性。

【问题讨论】:

    标签: vue.js


    【解决方案1】:
     <input v-model="name" type="text" class="form-control" />
    

    应该是

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

    因为您将数据定义为{newUser: {name: ""}}

    对您的其他字段执行相同操作,例如 email

    【讨论】:

    • 谢谢。我知道了。我忘了它是一个对象。
    猜你喜欢
    • 2022-01-24
    • 2019-08-08
    • 2018-07-13
    • 2018-05-29
    • 2019-03-30
    • 2019-06-25
    • 2017-06-16
    • 2018-06-22
    • 2020-09-02
    相关资源
    最近更新 更多