微信小程序-使用Component方法代替Page方法构造页面

一.使用Component方法的前提条件

在小程序js文件里使用Component方法代替Page方法需要在json文件里面定义usingComponents属性

复制代码
{
  "usingComponents": {}
}

二.注意事项

1.在page页面里使用的钩子函数和事件监听方法都需要写在methods对象里面

复制代码
  methods:{
    update(){
      this.setData({
        name:'jerry'
      })
    } ,
    onLoad(options){
      console.log(options)  
      console.log(this.data.id)
      console.log(this.properties.title)  
    }
  }
})

2.接收页面参数

2.1 通过onLoad(options)里面的options里面有接收的参数值

传参页面代码

复制代码
<navigator open-type="navigate" 
url="/pages/detail/detail?id=10&title=test">detail</navigator>

接收参数页面代码

复制代码
    onLoad(options){
      console.log(options)  
    }

2.2 通过properties接收参数,使用this.data或者this.properties获取接收的参数,前提得在properties对象里定义好参数,参数名和传参的参数名一致

复制代码
  properties:{
    id:{
      type:String,
      value:''
    },
    title:{
      type:String,
      value:''
    }
  }

    onLoad(options){
      console.log(this.data.id)
      console.log(this.properties.title)  
    }
相关推荐
咖啡八杯14 小时前
微信小程序人脸认证1.0迁移2.0
后端·微信小程序
xshirleyl14 小时前
微信小程序开发week8-慕尚花坊项目
微信小程序·小程序
admin and root17 小时前
Claude+Trae大模型 配置Chrome MCP联动Yakit自动化渗透测试
微信小程序·渗透测试·自动化·攻防演练·ai安全·claude code·ai自动化渗透测试
code_li1 天前
小程序上线需要的资质证书汇总
小程序·上线·发布·资质
hnxaoli2 天前
统信小程序(十三)循环键鼠操作程序
python·小程序
i查拉图斯特拉如是2 天前
使用workbuddy 30分钟搭建微信小程序
微信小程序·小程序
IceSugarJJ2 天前
Open-AutoGLM项目学习
语言模型·微信小程序·github
2501_916008892 天前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
android·macos·ios·小程序·uni-app·iphone·webview
咖啡の猫2 天前
小程序协同工作和发布
小程序
维双云2 天前
小程序怎么制作工具?与其盲目找开发,不如先分清自己要哪一种
小程序