Vue笔记(props和 mounted)

时间:2021-09-11 13:25:20

1.mounted

  1.1mounted中使用$nextTick会导致页面挂掉

 mounted() {
// 页面卡死
this.$nextTick(() => {
this.setUrl()
})
}

2.props

  2.1props传过去的值,第一时间在mounted是获取不到的。因为是异步传值的。

  解决方法:(1)使用watch;(2)将需要进行的处理在父组件进行操作,然后将操作完的值直接传给子组件。

 watch: {
datas: function (val) { }
}

(父)
<examAchSearchHeader :exprotUrl="exprotUrl"></examAchSearchHeader>
...
this.exportUrl = XXXX
(子)
props: {
exportUrl: String
}

  2.2通过props传给子组件的值变化后子组件接收到 和 通过refs访问子组件方法中使用接收到的参数变化的顺序问题

  通过refs访问时,接收到的参数是变化前的参数。还是因为异步的问题。可以强制赋值改变,或用watch等。

 // parent
<examAchTable ref="achTable" :dataList="examAchList"></examAchTable> // 若这里不强制赋值一下,在examAchList变化后直接调用子组件的transData方法时,子组件dataList仍是变化前的值
this.$refs.achTable.dataList = this.examAchList
this.$refs.achTable.transData(res.data.totalRecord) // child
transData(total) {
if (this.dataList)
// ...
}

持续更新中...