Problem background
Recently, I just started Vue, and there is a typical scene, which is also a simple pit. After requesting data through Axios, you need to give the data to tabledata in data, and the table component will automatically render according to the two-way binding of tabledata.
But when I assign a value, typeerror: cannot set property 'tabledata' of undefined
. What the hell is going on.
analysis
The code looks like there’s nothing wrong with it…
export default {
data() {
return {
total: 0, //Total number of default data
searchParam:{
limit: 10, //number of data items per page
page: 1, //default start page
certNumber: "",
certLevel: "",
certCompanyName: "",
},
tableData: []
};
},
onSearch: function(){
axios.post('/api/certCompany/list2',JSON.stringify(this.searchParam))
.then(function (response) {
console.log(response);
this.tableData=response.data.data;
this.total=response.data.count;
})
.catch(function (error) {
console.log(error);
});
}
};
However, the problem is this.tabledata
because the function
is used, in the function, this
points to the function itself, which is no longer the default this outside.
Solution:
Use a that
point Outsite this
, then call that.tableData
, easy to get.
onSearch: function(){
const that=this; //Use that to solve the function internal this pointing problem
axios.post('/api/certCompany/list2',JSON.stringify(this.searchParam))
.then(function (response) {
console.log(response);
that.tableData=response.data.data;
that.total=response.data.count;
})
.catch(function (error) {
console.log(error);
});
}
Similar Posts:
- [Solved] Uniapp Axios appears in wechat applet index.js? [SM]: 33 errortyerror: l is not a function error
- Two solutions to cross origin read blocking (CORB) blocked cross origin response error of Web Service API
- [Solved] react native TypeError: Network request failed Unable to symbolicate stack trace: The stack is null
- [Solved] Axios Cross-domain issues: No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
- How to Solve Request failed with status code 404
- Uncaught SyntaxError: Unexpected token o
- [Solved] Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
- How to Solve Error: Unexpected token o in JSON at position 1
- Ajax “SyntaxError: missing ; before statement” [How to Solve]