Vue uses prop to pass values, and the child component does not get the values

there is a parent component and a child component,

in the parent component, a data is obtained from the database and stored in a map,

then props this data to the subcomponent, which is a map, initializes the map directly on the lifecycle mounted and uses the properties of props to load mark points.

but it is found that the map is loaded and the mark point is not loaded. Finally, it is found that when the subcomponent mounted is initialized, the property of props does not have a value.

how to solve this problem:

the parent component gets data from the database

      /* */
      async initWzDataFromServer() {
        let data = await getInitData() || []
        data.result.map(item => {
          this.wzSocket.set(item.deviceSn, item)
        })
      },

pass to child components

 <MapMark :initWzData="wzSocket"/>

subcomponents get props

          init() {
        this.initMap()
        this.initMarkerData()
      },


    mounted() {
      this.init()
    },

I hope you can help

add that this is watch

    watch: {
      /* initWzData*/
      localWzData: function (newValue) {
        this.initMarkerData()
        // for (let value of newValue.values()) {
        //   console.log(value)
        // }
      }
    },

this is data

    data() {
      return {
        map: null,
        localWzData: this.initWzData
      }
    },
Mar.22,2021

console.log('')

use watch , refer to the document ide/computed.html-sharp%E4%BE%A6%E5%90%AC%E5%99%A8" rel=" nofollow noreferrer > listener .


I just encountered a similar problem.


use v-if for child components to ensure that the properties of prop have a value before rendering


problem is solved, mainly because my data structure uses map, so I can not monitor the change. Later, I changed the map into an array, and I can monitor that the data is obtained


async initWzDataFromServer() {
  let data = await getInitData() || []
  this.wzSocket = new Map(
    data.result.map(item => [item.deviceSn, item])
  )
},
.
Menu