Within this component, I have a method that updates an employee. My goal is to display the error message in the view immediately after the "errorMessage" variable is assigned/changed within the "error" callback of the 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;
}
});
}
View:
<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>