The problem of this in ts

in the ts file, it is supposed to intercept the error, and then pop up a message, but calling this here will report an error.

// 
axiosTokenInstance.interceptors.response.use((response: any) => {
  if (response.data.statusCode !== 0) {
    this.$Message.error("haha");
  } else {
    return response.data;
  }
},
  (error) => {
    if (error.response) {
      const { message } = error.response.data;
      return Promise.reject(new Error(message));
    }
    return Promise.reject(error);
  }
);

then modified the code

const that = this;

// 
axiosTokenInstance.interceptors.response.use((response: any) => {
  if (response.data.statusCode !== 0) {
    that.$Message.error("haha");
  } else {
    return response.data;
  }
},
  (error) => {
    if (error.response) {
      const { message } = error.response.data;
      return Promise.reject(new Error(message));
    }
    return Promise.reject(error);
  }
);
After the

modification, the outer this is marked red again [ts] "this" implicitly has the type "any" because it has no type comments. [2683] who can tell us how to solve the problem? /

Jan.12,2022

ts provides / / @ ts-ignore to mark the user the code to ignore the check

but pay attention to the object reference of this, which is different from the this reference of function () {} and () = > {}. Since @ ts-ignore is used, you must judge and debug the code correctly before submitting the code, and @ ts-ignore should not be abused, otherwise you will lose the original intention of ts.

https://www.tslang.cn/docs/ha.


  

Hello, landlord, have you solved it?
I have encountered a similar problem. If it is solved, can you tell me how to do it?

Menu