1. Method createAnimation calls method getToken,getToken sends a post request and assigns the returned parameter to the variable token, in data (), but the value of token cannot be obtained with this.token in createAnimation
2,
createAnimation() {
const t = this;
this.viewDialog = true;
this.viewTitle = this.anaInput;
t.loading = true;
t.getToken()
// console.log(t.getToken());
// this.$options.methods.getToken.bind(this)();
console.log(222,t.token)
},
getToken() {
// this.token="qqqq";
this.$http.post(this.ip + "/getyoyatoken", { //token
userid: this.user_id
}).then((res) => {
if (res.data.data.result === 1) {
this.token = res.data.data.list[0].token;
// return res.data.data.list[0].token;
console.log(111,this.token)
}
})
},
3. According to Baidu"s use of this.$options.methods.getToken.bind (this) (); or this.$options.methods.getToken (); does not work, and error post undefined, I tried to assign values to token directly in getToken, which can be obtained in createAnimation. It is true that the value of the variable token has been changed and cannot be obtained in the createAnimation method, but you can get the value of token by performing other methods after executing createAnimation, and I tried to use setTimeout (() = > {this.getToken}). If you directly ask getToken to return back the response result, the received result will be printed out in createAnimation as undefined
.4. What is the reason for this?
5, add:
async createAnimation() {
const t = this;
this.viewDialog = true;
this.viewTitle = this.anaInput;
t.loading = true;
t.token = await t.getToken();
console.log(222,t.token)
}
you can"t get token, if you write it this way, because you can call the getToken method in many places, so how can it be easier? you don"t have to rewrite those lines of code in getToken
.