在Vue js中显示错误消息 [英] Show error message in Vue js

查看:136
本文介绍了在Vue js中显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a following component in which I have a method which updates employee. I want to show the error message in view as soon as erroMessage variable is assigned/changed in "error" callback of ajax call.

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>


推荐答案

因为你丢失了这个 中的引用错误:function(){}

您可以使用箭头功能:

You can use arrow functions:

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

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

or if you don't want to use ES6, you can specify the context option in $.ajax()

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

或只是保留参考

updateEmployee: function () {
  var _this = this;

...

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

这篇关于在Vue js中显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆