Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem when using props to pass values in mounted processing?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要介绍了使用props传值时无法在mounted处理怎么解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇使用props传值时无法在mounted处理怎么解决文章都会有所收获,下面我们一起来看看吧。

props传值无法在mounted处理的解决遇到的问题

父组件传值,在子组件中不能用mounted处理

export default{ props:['floor1'], data(){ return { floor1_0:'', floor1_1:'', floor1_2:'', } }, mounted(){ console.log(this.floor1) //打印出的不是所传的值 this.floor1_0 = this.floor1[0]; }}

因为props为异步传值(就是在父组件没有加载完数据时,floor1就传递到了子组件,此时floor1还没被附上值,先执行了子组件的mounted),而mounted执行一次后无法改变floor1的值。

解决

使用侦听器watch,当floor1改变时,重新计算

watch:{ floor1:function(val){ this.floor1_0 = val[0]; this.floor1_1 = val[1]; }}vue笔记(props和mounted)1.mounted

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

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

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

解决方法:

(1)使用watch

(2)将需要进行的处理在父组件进行操作,然后将操作完的值直接传给子组件。

watch: { datas: function (val) { } }或(父) ...this.exportUrl = XXXX(子)props: { exportUrl: String}

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

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

// parent // 若这里不强制赋值一下,在examAchList变化后直接调用子组件的transData方法时,子组件dataList仍是变化前的值 this.$refs.achTable.dataList = this.examAchList this.$refs.achTable.transData(res.data.totalRecord) // child transData(total) { if (this.dataList) // ...}关于"使用props传值时无法在mounted处理怎么解决"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"使用props传值时无法在mounted处理怎么解决"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report