Let me summarize a problem that I have encountered for a long time. In the project, the background data is needed to render the front end. Axios integrated with Vue is used. The hook function in Vue is used to send a get request to the background after the page component is mounted, and then the returned data is assigned to the attributes defined in data(): After execution, the front end reports an error:reason:After the request is successfully executed, the contents of the callback function are executed. The callback function is inside other functions and this is not bound to any object and is undefined. Solution:1) Assign this pointing to the Vue object to the property defined by the external method, and then use the property in the internal method 2) Using arrow functions Supplement: Solve the undefined problem of calling between data in vue data Solution:There is no solution, it cannot be called like this at all. Although this in the data function points to VueComponent (understanding: the data in data can use this to call the data in props), when calling another attribute in data, the data in data has not been parsed yet, because when returning the {} object, all the data in them are rendered and parsed together, so the undefined problem will occur. (The above is just my personal understanding. If there are any errors, please comment and correct them.) So choose to complete the assignment operation in the mounted life cycleexport default { data(){ return { firstName:'111', lastName:'222', fullName:'' } }, mounted(){ this.fullName = this.firstName + '' + this.lastName; } } Display results: Of course, if fullName does not need to be defined in data, it may be elegant to define it in the computed property. The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me. You may also be interested in:
|
<<: Linux lossless expansion method
>>: MySQL turns off password strength verification
Today I will introduce a small Javascript animati...
This article shares the specific code for JavaScr...
As shown below: Yesterday: UNIX_TIMESTAMP(CAST(SY...
Table of contents 1. Create HTML structure 2. Cre...
Table of contents JavaScript events: Commonly use...
1. Download Dependency npm install @antv/data-set...
Table of contents General upload component develo...
This article mainly introduces the process of imp...
When we develop a web project with Django, the te...
Table of contents After creating a container loca...
Table of contents ReactRouterV6 Changes 1. <Sw...
environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...
Macrotasks and Microtasks JavaScript is a single-...
mysql basic data types Overview of common MySQL d...
Table of contents 1. What content usually needs t...