[Solved] Vue Baidu map loading error: cannot read properties of undefined (reading ‘centerandzoom’)

Because the map positioning function needs to be used in the project, then I made a reference to the Vue Baidu map document, which can obtain the location name and location coordinates by clicking the map, and then save them to the database. When users edit the location information, they can locate the corresponding geographical location on the map through the coordinates stored in the database. However, when I finish the function, I find that there will be an error message when I load it for the first time, but the function is normal, and there will be no error message when I operate later

No more nonsense, let’s talk about the solution first:

Change mounted() to obtain the coordinate data stored in the database, and request the coordinate data when changed to created(). The following is a detailed description:

There are a lot of error messages, as shown below:

Code part

Here I got the coordinate data in the database from other pages and stored it in vuex, and then fetched the value from vuex, here can also be changed to use axios to send a request to get the coordinate data stored in the database.┬áThe value of form.csr_address is the “storage address”, and form.lng and form.lat are latitude and longitude.

Finally, I found that because the data is obtained during mounting, but Baidu map rendering seems to be before mounting, it will cause an error if it has not obtained the data from vuex when it calls the coordinate data. Then change mounted() to created() to solve the problem.

Similar Posts: