开放式跨端跨组件解决方案,持使用 React/Vue/Nerv 等框架来开发 微信 / 京东 / 百度 / 支付宝 / 字节跳动 / QQ / 飞书 / 快手 小程序 / H5 / RN 等应用。
ts
体验AI代码助手
代码解读
复制代码
import React, {Component, useState, useEffect} from 'react';
一个入口组件(app.js)伴随一个全局配置文件(app.config.js)。
一个页面组件(index.jsx)也会有一个页面配置(index.config.js)。
ts
体验AI代码助手
代码解读
复制代码
`export default defineAppConfig({
pages: [
'pages/index/index',
'pages/hot/hot',
'pages/latest/latest',
],
tabBar: {
color: '#000',
selectedColor: '#fff',
list: [
{
iconPath: 'resource/index.png',
selectedIconPath: 'resource/index_on.png',
pagePath: 'pages/index/index',
text: '首页',
},
{
iconPath: 'resource/hotest.png',
selectedIconPath: 'resource/hotest_on.png',
pagePath: 'pages/hot/hot',
text: '最热',
},
{
iconPath: 'resource/latest.png',
selectedIconPath: 'resource/latest_on.png',
pagePath: 'pages/latest/latest',
text: '最新',
},
],
},
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black',
}
})`
属性
类型
必填
默认值
描述
color
HexColor(十六进制颜色值)
是
tab 上的文字默认颜色,仅支持十六进制颜色
selectedColor
HexColor(十六进制颜色值)
是
tab 上的文字选中时的颜色,仅支持十六进制颜色
backgroundColor
HexColor(十六进制颜色值)
是
tab 的背景色,仅支持十六进制颜色
borderStyle
String
是
black
tabbar 上边框的颜色, 仅支持 black / white
list
Array
是
tab 的列表,详见 list 属性说明,最少 2 个、最多 5 个 tab
position
String
否
bottom
tabBar 的位置,仅支持 bottom / top
custom
Boolean
否
false
自定义 tabBar
ts
体验AI代码助手
代码解读
复制代码
`import {onError} from '@tarojs/taro'
onError(error => {
console.log(error);
})`
ts
体验AI代码助手
代码解读
复制代码
`import {onPageNotFound} from '@tarojs/taro'
onPageNotFound(() => {
console.log('onPageNotFound');
})`
ts
体验AI代码助手
代码解读
复制代码
`import {onUnhandledRejection} from '@tarojs/taro'
onUnhandledRejection(rejection => {
console.log('onUnhandledRejection', rejection);
})`
ts
体验AI代码助手
代码解读
复制代码
`import { View, Text } from '@tarojs/components'
import './hot.less'
export default class Hot {
onLoad (options) {
console.log('onload',options)
}
render() {
return (
<View className='hot'>
<Text>Hot!</Text>
</View>
)
}
}`
ts
体验AI代码助手
代码解读
复制代码
`onUnload () {
console.log('onunload');
}`
只有在页面组件才会触发onReady生命周期。子组件可以使用Taro内置的消息机制监听页面的onReady()生命周期。
ts
体验AI代码助手
代码解读
复制代码
`componentDidShow () {
console.log('componentDidShow');
}`
ts
体验AI代码助手
代码解读
复制代码
`componentDidHide () {
console.log('componentDidHide');
}`
监听用户下拉动作。
监听用户上拉触底事件。
ts
体验AI代码助手
代码解读
复制代码
`onAddToFavorites (res) {
console.log('res',res);
return {
title: 'AAA',
imageUrl: '',
query:''
}
}`
ts
体验AI代码助手
代码解读
复制代码
`//hot.config.ts
export default {
enableShareAppMessage:true
}`
ts
体验AI代码助手
代码解读
复制代码
`onShareAppMessage(res) {
console.log(res);
if (res.from === 'menu') {
// 来自页面内转发按钮
console.log(res.target)
}
return {
title: 'AAA',
path: 'pages/hot/hot' ,
}
}`
参数
类型
说明
from
String
转发事件来源。 button:页面内转发按钮; menu:右上角转发菜单
target
Object
如果 from
值是 button
,则 target
是触发这次转发事件的 button
,否则为 undefined
webViewUrl
String
页面中包含 WebView
组件时,返回当前 WebView
的 url
字段
类型
说明
title
转发标题
当前小程序名称
path
转发路径
当前页面 path ,必须是以 / 开头的完整路径
imageUrl
自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径。支持 PNG 及 JPG 。显示图片长宽比是 5:4
使用默认截图
ts
体验AI代码助手
代码解读
复制代码
`onShareAppMessage(res) {
console.log(res);
if (res.from === 'menu') {
// 来自页面内转发按钮
console.log(res.target)
}
return {
title: 'AAA',
path: 'pages/hot/hot' ,
}
}
onShareTimeline() {
console.log('onShareTimeline');
return {
title: 'AAA',
query: ''
}
}`
ts
体验AI代码助手
代码解读
复制代码
`onTabItemTap(res) {
console.log('onTabItemTap',res);
}`
参数
类型
说明
index
String
被点击 tabItem 的序号,从 0 开始
pagePath
String
被点击 tabItem 的页面路径
text
String
被点击 tabItem 的按钮文字
ts
体验AI代码助手
代码解读
复制代码
`import {usePageScroll, useReachBottom} from '@tarojs/taro';
import {useState, useEffect} from 'react';`
ts
体验AI代码助手
代码解读
复制代码
`import { PropsWithChildren } from 'react'
import { useLaunch } from '@tarojs/taro'
import './app.less'
function App({ children }: PropsWithChildren<any>) {
useLaunch(options => {
console.log('useLaunch',options);
})
// children 是将要会渲染的页面
return children
}
export default App`
属性
类型
说明
path
string
启动小程序的路径
scene
number
启动小程序的场景值
query
object
启动小程序的query参数
referrerInfo
object
来源信息。从另一个小程序、公众号或App进入小程序时返回,否则返回{}
shareTicket
string
tsx
体验AI代码助手
代码解读
复制代码
`import { View, Text, CoverImage } from '@tarojs/components'
import './latest.less'
export default function News() {
return (
<View className='latest'>
<Text>News!</Text>
<CoverImage src='https://img2.baidu.com/it/u=2124848576,3207513143&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1718989200&t=a8243be687d5c968096c541d713209d8'></CoverImage>
</View>
)
}`
tsx
体验AI代码助手
代码解读
复制代码
`export default function Index() {
return (
<View className='index'>
<CustomWrapper>
<Text>Hello world!</Text>
</CustomWrapper>
</View>
)
}`
tsx
体验AI代码助手
代码解读
复制代码
`//News!只有在高度达到30000px时才会展示
export default function News() {
return (
<View className='hot'>
<MatchMedia minHeight={30000}>
<Text>News!</Text>
</MatchMedia>
<CoverImage src='https://img2.baidu.com/it/u=2124848576,3207513143&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1718989200&t=a8243be687d5c968096c541d713209d8'></CoverImage>
</View>
)
}`
参数
类型
必填
说明
minWidth
number
否
页面最小宽度( px 为单位)
maxWidth
number
否
页面最大宽度( px 为单位)
width
number
否
页面宽度( px 为单位)
minHeight
number
否
页面最小高度( px 为单位)
maxHeight
number
否
页面最大高度( px 为单位)
height
number
否
页面高度( px 为单位)
orientation
string
否
屏幕方向( landscape 或 portrait )
粉色块可拖拽
tsx
体验AI代码助手
代码解读
复制代码
`export default function News() {
return (
<View className='hot'>
<MovableArea style='height: 200px; width: 200px; background: skyblue;'>
<MovableView style='height: 70px; width: 70px; background: pink;' direction='all'>Movable</MovableView>
</MovableArea>
</View>
)
}`
点击之后:
tsx
体验AI代码助手
代码解读
复制代码
`export default function Index() {
const [show, setShow] = useState(false)
const toggle = () => {
setShow(!show)
}
return (
<View className='index'>
<Button onClick={toggle}>显示root-portal</Button>
{
show && (<RootPortal><View>content</View></RootPortal>)
}
</View>
)
}`
tsx
体验AI代码助手
代码解读
复制代码
`export default function Index() {
return (
<Text>Hello world!</Text>
<Swiper style={{height:300}} indicatorColor='#999' indicatorActiveColor='#333' vertical circular indicatorDots autoplay>
<SwiperItem>
<View><CoverImage src='https://img2.baidu.com/it/u=2124848576,3207513143&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1718989200&t=a8243be687d5c968096c541d713209d8'></CoverImage> </View>
</SwiperItem>
<SwiperItem>
<View><CoverImage src='https://img0.baidu.com/it/u=508513605,220007307&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1718989200&t=fad0fd4334133b50dfe55a1219e5b933'></CoverImage> </View>
</SwiperItem>
</Swiper>
</View>
)
}`
参数
说明
success
成功图标
success_no_circle
成功图标(不带外圈)
info
信息图标
warn
警告图标
waiting
等待图标
cancel
取消图标
download
下载图标
search
搜索图标
clear
清除图标
circle
圆环图标(多选控件图标未选择)「微信文档未标注属性」
info_circle
带圆环的信息图标「微信文档未标注属性」
tsx
体验AI代码助手
代码解读
复制代码
`export default function News() {
return (
<View className='hot'>
<Icon size='60' type='success' />
<Icon size='60' type='info' />
<Icon size='60' type='warn' color='#ccc' />
<Icon size='60' type='warn' />
<Icon size='60' type='waiting' />
<Icon size='20' type='success_no_circle' />
<Icon size='20' type='warn' />
<Icon size='20' type='success' />
<Icon size='20' type='download' />
<Icon size='20' type='clear' color='red' />
<Icon size='20' type='search' />
</View>
)
}`
tsx
体验AI代码助手
代码解读
复制代码
`import { View, RichText } from '@tarojs/components'
//使用ts时引入node数据类型
import { RichTextProps } from '@tarojs/components/types/RichText'
export default function Index() {
const nodes: RichTextProps['nodes'] =[{
name: 'div',
attrs: {
class: 'div_class',
style: 'line-height: 60px; color: pink;'
},
children: [{
type: 'text',
text: 'Hello World!'
}]
}]
return (
<View className='index'>
<RichText nodes={nodes} />
</View>
)
}`
参考:Taro官方文档
原网址: 访问
创建于: 2025-07-17 18:22:08
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论