Vue async和await的使用

发布于:2023-01-06 ⋅ 阅读:(509) ⋅ 点赞:(0)

 data(){

name:"456",

},

mouted(){

this.Query();

},

methods:{

async Query(){
                let se;

//后台接口

let url="/api/路径";
                //this.http.post(url, "查询中").then(result => {
                //    if (result.status == true) {
                //        this.name = "123";
                //        console.log("不使用异步"+this.name)
                //    }
                //})
                se = await new Promise((resolve) => {
                    this.http.post(url, "查询中").then(result => {
                        if (result.status == true) {
                            this.name = "123";
                            console.log("使用异步"+this.name)
                            resolve("123");
                        }
                    })

                })
                this.Output();
            },
         
            Output() {
                console.log(this.name);
            },

}

打印结果,使用异步获取改变的值。


网站公告

今日签到

点亮在社区的每一天
去签到