Vue.js 在axios获取响应后在html中插入值
我有这样的代码:Vue.js 在axios获取响应后在html中插入值,vue.js,axios,Vue.js,Axios,我有这样的代码: <div class="col-md-4" id="app2"> <div v-if="post && post.length"> {{post}} </div> <input class="form-control" type="text" v-model="message" placeholder="Enter E
<div class="col-md-4" id="app2">
<div v-if="post && post.length">
{{post}}
</div>
<input class="form-control" type="text" v-model="message" placeholder="Enter Email..." />
<button class="btn btn-secondary" type="submit" v-on:click="sendEmail">Subscribe!</button>
</div>
当axios收到回复时,如何在html中插入帖子?因此,如果响应为201,则为任意字符串,如果不是201,则为错误值?
此
在您的函数中不是Vue
实例
你应该使用箭头功能
var app2 = new Vue({
el: '#app2',
data: {
message: null,
post: [],
},
methods: {
sendEmail: function () {
var bodyFormData = new FormData();
bodyFormData.set('email', this.message);
axios({
method: 'post',
url: '/api/subsc/',
data: bodyFormData,
})
.then((response) => {
if (response.status == 201) {
console.log(response);
this.post = "Bla bla bla";
}
})
.catch((error) => {
this.post = error.response.data.email[0];
});
}
}
})
或
var app2 = new Vue({
el: '#app2',
data: {
message: null,
post: [],
},
methods: {
sendEmail: function () {
var bodyFormData = new FormData();
bodyFormData.set('email', this.message);
axios({
method: 'post',
url: '/api/subsc/',
data: bodyFormData,
})
.then((response) => {
if (response.status == 201) {
console.log(response);
this.post = "Bla bla bla";
}
})
.catch((error) => {
this.post = error.response.data.email[0];
});
}
}
})
var app2 = new Vue({
el: '#app2',
data: {
message: null,
post: [],
},
methods: {
sendEmail: function () {
var vm = this;
var bodyFormData = new FormData();
bodyFormData.set('email', this.message);
axios({
method: 'post',
url: '/api/subsc/',
data: bodyFormData,
})
.then(function (response) => {
if (response.status == 201) {
vm.post = "Bla bla bla";
}
})
.catch(function (error) => {
vm.post = error.response.data.email[0];
});
}
}
})