Javascript 解决axios get请求后如何修改对象
在发出axios get请求后,我正在Vue中设置对象Javascript 解决axios get请求后如何修改对象,javascript,vue.js,axios,es6-promise,Javascript,Vue.js,Axios,Es6 Promise,在发出axios get请求后,我正在Vue中设置对象containerInfo的res.data,如下所示: methods: { searchContainers(containerSearch) { this.containerQuery = JSON.parse(containerSearch.container_query)[0]; this.imageBranch = JSON.parse(containerSearch.container_query
containerInfo
的res.data,如下所示:
methods: {
searchContainers(containerSearch) {
this.containerQuery = JSON.parse(containerSearch.container_query)[0];
this.imageBranch = JSON.parse(containerSearch.container_query)[1];
this.containerInfo["image_branch"] = this.imageBranch
this.url = this.containerQuery.split('/');
axios.get(`http://localhost:8000/get?name=${this.url[0]}/${this.url[1]}/${this.url[2]}&ver=${this.url[3]}`)
.then(res => this.containerInfo = res.data)
.then(this.containerInfo = [...this.containerInfo, this.imageBranch]);
}
在vue中接收/设置数据对象后,我想将this.imageBranch
添加到containerInfo
对象中
问题是axios res一旦收到(需要几秒钟),就会删除this.imageBranch
键/附加值。我知道我应该在承诺解决后再加上,但我不知道怎么加
有人能帮忙吗 不要使用两个
。然后只使用一个,并在arrow函数中执行所有代码,如下所示:
methods: {
searchContainers(containerSearch) {
this.containerQuery = JSON.parse(containerSearch.container_query)[0];
this.imageBranch = JSON.parse(containerSearch.container_query)[1];
this.containerInfo["image_branch"] = this.imageBranch
this.url = this.containerQuery.split('/');
axios.get(`http://localhost:8000/get?name=${this.url[0]}/${this.url[1]}/${this.url[2]}&ver=${this.url[3]}`)
.then(res => {
this.containerInfo = res.data;
this.containerInfo = [...this.containerInfo, this.imageBranch]
});
}
我建议使用清晰直观的wait/async语法。所以代码应该是这样的
async searchContainers() {
const res = await axios.get(`http://localhost:8000/get?name=${this.url[0]}/${this.url[1]}/${this.url[2]}&ver=${this.url[3]}`);
this.containerInfo = res.data;
this.containerInfo = [...this.containerInfo, this.imageBranch];
}
我想出来了。需要在对象中设置键:this.containerInfo.image\u branch=this.imageBranch
您需要将函数传递给。然后
。。。其他任何内容都会被忽略(请参见第二个。然后
没有被传递函数)您能否提供一个@Bravo示例?第一个。然后
传递了一个函数作为参数,第二个,我没有尝试您建议的,但仍然收到相同的结果:(@swar35-取决于您在哪里检查this.containerInfo
我已经尝试了上述所有建议。我的this.imageBranch
会在res发送后不断被删除。为什么不干脆this.containerInfo=[…this.res.data,this.imageBranch];
即使使用wait/async语法,我也会继续得到相同的结果