温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用props传值时无法在mounted处理怎么解决

发布时间:2022-04-24 10:56:20 来源:亿速云 阅读:296 作者:iii 栏目:开发技术

这篇文章主要介绍了使用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) {
      
    }
  }
或
(父)
 <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) 
    // ...
}

关于“使用props传值时无法在mounted处理怎么解决”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“使用props传值时无法在mounted处理怎么解决”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI