javascript - 在Vue js中显示错误消息

标签 javascript vue.js

我有一个以下组件,其中有一个更新员工的方法。我想在 ajax 调用的“错误”回调中分配/更改 erroMessage 变量后立即在 View 中显示错误消息。

var EmployeeEdit = Vue.extend({
    template: '#employee-edit',
    data: function () {
        return {employee: findEmployee(this.$route.params.employee_id),errorMessage:'as'};
    },
    methods: {
        updateEmployee: function () {
            var employee = this.employee;
            $.ajax({
                url: "/vue/employee/update",
                type: "POST",
                data:{
                    id: employee.id,
                    name: employee.name,
                    profile: employee.profile,
                    age: employee.age
                },
                success: function (data) {
                    router.push('/');
                },
                error:function (xhr, status, error) {
                    console.log("message....... " + xhr.responseText);
                    this.errorMessage =  xhr.responseText;
                }
            });

        }

查看:

<template id="employee-edit">

    <section>
        <header class="page-header">
            <div class="row">
                <div class="col-sm-4">
                    <h1>Edit Employee</h1>
                </div>
            </div>
        </header>
        <p >{{ errorMessage }}</p>
        <form v-on:submit="updateEmployee">
            <div class="form-group">
                <label for="edit-name">Name</label>
                <input class="form-control" id="edit-name" v-model="employee.name" required/>
            </div>
            <div class="form-group">
                <label for="edit-description">Profile</label>
                <textarea class="form-control" id="edit-description" rows="3" v-model="employee.profile"></textarea>
            </div>
            <div class="form-group">
                <label for="edit-price">Age</label>
                <input type="number" class="form-control" id="edit-price" v-model="employee.age"/>
            </div>
            <button type="submit" class="btn btn-primary">Save</button>
            <router-link to="/" class="btn btn-default">Cancel</router-link>
        </form>
    </section>
</template>

最佳答案

因为您在 error:function(){} 中丢失了 this 引用

您可以使用箭头函数:

error: (xhr, status, error) => {
  console.log("message....... " + xhr.responseText);
  this.errorMessage = xhr.responseText;
}

或者如果你不想使用ES6,你可以在$.ajax()中指定context选项

$.ajax({
  context: this,
  ...

或者只是保留this引用

updateEmployee: function () {
  var _this = this;

...

  error: function (xhr, status, error) {
    console.log("message....... " + xhr.responseText);
    _this.errorMessage =  xhr.responseText;
  }

关于javascript - 在Vue js中显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43516440/

相关文章:

javascript - 将 "this"传递到嵌套数据函数 Vuejs

vue.js - Vuex 可重用模块模式。对状态不工作使用函数

javascript - Tabulator 5.0 - 解析日期时间 luxon - 日期时间 ISO 8601

vue.js - Vue 分页 - 如何使用它?

javascript - 获取父元素以外的元素的偏移量?

javascript - 选择器仅在 setTimeout 中找到合适的值

javascript - 组件名称无效 : "pages/product/_slug.vue". 组件名称应符合 html5 规范中有效的自定义元素名称

javascript - 如何先从服务器加载对象 - vuejs - vuex

javascript - JQuery 将错误的值附加到变量

javascript - 使用javascript将特殊字符转换回HTML实体代码