Ray 开发框架——小程序跨端API集合

本文介绍了跨端API在项目中的应用,包括usePageEvent和withPageLifecycle的生命周期管理,以及router和location对象的路由控制。推荐在项目中采用FunctionComponents和HooksAPI进行开发。
摘要由CSDN通过智能技术生成

跨端 API

跨端框架提供给项目统一的运行环境,实现多端能力的一致性,包含路由,生命周期。

使用方式:

import { router } from 'ray';

API 概览

import { usePageEvent, withPageLifecycle, router, location } from 'ray';

usePageEvent

页面生命周期 hook,可用于监听页面生命周期。

签名:usePageEvent(event: string, callback: Function);

event:

  • onShow 页面可见时
  • onHide 页面隐藏时
  • onPageScroll 页面滚动时
  • onPullDownRefresh 下拉刷新
  • onReachBottom 页面触底
  • onResize 页面尺寸发生改变
  • onLoad 页面加载时触发
  • onUnload 页面卸载时触发
  • onReady 页面初次渲染完成时触发
  • onShareAppMessage 页面分享(微信小程序独有)

针对 Class Components,在跨端运行模式下默认扩展以上生命周期。示例:

import React from 'react';
import { View } from '@ray-js/ray';
 
export default class Home extends React.Component {
  onShow() {
    // page show
  }
  render() {
    return <View>Home Page</View>;
  }
}

withPageLifecycle

扩展生命周期能力,可用于子组件(Class Components)中监听页面生命周期。示例:

import React from 'react';
import { View } from '@ray-js/ray';
import { withPageLifecycle } from 'ray';
 
class Child extends React.Component {
  onShow() {
    // 所在页面触发 page.onShow
  }
  render() {
    return <View>Child</View>;
  }
}
 
export default withPageLifecycle(Child);

推荐项目开发过程中均使用 Function Components 写法,以及 Hooks API。

router

router 对象实现跨平台统一的路由跳转方式,有以下用法:

  • router.push(url: string)
  • router.replace(url: string)
  • router.back()
  • router.go(delta: number)
  • router.reload()

location

location 对象

  • hash hash (web 特有)
  • host URL 域名含端口(web 特有)
  • hostname URL 域名 (web 特有)
  • port URL 端口(web 特有)
  • protocol URL 协议(web 特有)
  • href 页面全路径
  • pathname URL 路径部分
  • search URL 参数
  • query: Record<string, string> URL 参数对象
  • params: Record<string, string> 匹配表达式路由参数,
    • 表达式路由 /detail/:uid

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值