【发布时间】:2017-11-07 16:26:32
【问题描述】:
我在 VueJS 中有方法和创建的属性
created() {
axios.get('/wp-json/wp/v2/users/' + portal.user.id + '?context=edit', {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
this.user = response.data;
})
.catch(e => {
this.errors.push(e);
});
axios.get('/wp-json/wp/v2/categories')
.then(response => {
this.terms = response.data;
})
.catch(e => {
this.errors.push(e);
});
},
methods: {
createPost: function() {
let title = this.new_post_title;
let content = this.new_post_content;
let tags = this.new_post_tags;
let status = this.status;
let data = {
'title': title,
'content': content,
'status': status,
};
axios.post("/wp-json/wp/v2/posts/", data, {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
console.log(response);
})
.catch(e => {
this.errors.push(e);
console.log(this.errors);
});
this.new_post_title = '';
this.new_post_content = '';
this.new_post_tags = '';
}
},
一切都在处理请求,数据正在发布到 WordPress,当我进行页面刷新时,我会在页面顶部看到应有的新帖子。
但是在请求完成后,如何让页面异步加载新帖子?
【问题讨论】:
-
如果发布请求成功,那么我只需将发布的数据插入到带有 Vue 的页面中,在
then回调中。 -
你能用一个小代码sn-p详细说明一下吗?我现在才使用 Vue 大约一个月。
-
是的。哪个网址获取帖子?
'/wp-json/wp/v2/categories'? -
/wp-json/wp/v2/posts?context=edit&_embed=true
-
那么您列出帖子的组件与您创建帖子的组件不同?因为我在您的示例中没有看到 get 请求。
标签: javascript rest api vue.js axios