1***@126.com
1***@126.com
  • 发布:2019-03-03 13:14
  • 更新:2019-10-13 21:12
  • 阅读:19333

uni-app自定义组件传值,onload里为何不好用?

分类:uni-app

请教:父组件传值artId,在子组件author中模板能显示,但是在onload中url:this.apiServer + 'art&m=articleAuthor&artid='+artId,为空,换上具体数值比如59就好用。小白一个,请各位大大帮忙看看,谢谢!
子组件:
<template name="author">
<view>
来自:{{authorinfo.u_name}}发布时间:{{authorinfo.art_createtime}}
<!--{{artId}}此处artId是能显示的-->
</view>
</template>
<script>
var authorinfo, artId;
export default {
name : "author",
data() {
return {
authorinfo :[]
};
},
props:{
artId:1

    },  
    onLoad:function(option){  
        artId = option.artId;  
        uni.request({  
            url:this.apiServer + 'art&m=articleAuthor&artid='+artId,  
            method: 'GET',  
            success: res => {  
                if(res.data.status == 'ok'){  
                    console.log(res.data);  
                    this.authorinfo = res.data.data;  
                }  
            }  
        })  
    }  

}
</script>
父组件没问题代码节略:
<author :artId="item.art_id"></author><!--item.art_id有值-->
<script>
import author from "../../components/author.vue";
components:{author}
</script>

2019-03-03 13:14 负责人:无 分享
已邀请:
zzk

zzk

只有页面才有onLoad onShow
组件和vue一样用mounted吧

  • 1***@126.com (作者)

    谢谢回复,可以用吧?我在官方文档中uni-app组件的常见问题中看到可以用,在项目中也没问题,只是artId在onload中失效了

    2019-03-03 13:42

1***@126.com

1***@126.com (作者)

把onload改成computed,终于好用了
computed:{
show (option){
artId = option.artId;
uni.request({
url:this.apiServer + 'art&m=articleAuthor&artid='+artId,
method: 'GET',
success: res => {
if(res.data.status == 'ok'){
console.log(res.data);
this.authorinfo = res.data.data;
return this.authorinfo;
}
}
})
},
}

1***@qq.com

1***@qq.com - 还是小幸运呀~

为什么我改了,不好用呢

SeeMyEyes

SeeMyEyes - 程序员

组件会使uniapp的生命周期失效,换成vue的生命周期就行了

不老刘

不老刘

我的onload里面要接收页面参数,然后通过请求后台获得一个返回值。要把这个返回值传到子组件。
没法改成computed啊

该问题目前已经被锁定, 无法添加新回复