Taro快速入门目录

Taro 多端统一开发框架,支持用 React 的开发方式编写一次代码,生成能运行在微信小程序/百度智能小程序/支付宝小程序、H5、React Native 等的应用。

Taro 特性

React 语法风格

Taro 的语法规则基于 React 规范,它采用与 React 一致的组件化思想,组件生命周期与 React 保持一致,同时在书写体验上也尽量与 React 类似,支持使用 JSX 语法,让代码具有更丰富的表现力。 代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Taro, { Component } from '@tarojs/taro'
import { View, Button } from '@tarojs/components'

export default class Index extends Component {
constructor () {
super(...arguments)
this.state = {
title: '首页',
list: [1, 2, 3]
}
}

componentWillMount () {}

componentDidMount () {}

componentWillUpdate (nextProps, nextState) {}

componentDidUpdate (prevProps, prevState) {}

shouldComponentUpdate (nextProps, nextState) {
return true
}

add = (e) => {
// dosth
}

render () {
return (
<View className='index'>
<View className='title'>{this.state.title}</View>
<View className='content'>
{this.state.list.map(item => {
return (
<View className='item'>{item}</View>
)
})}
<Button className='add' onClick={this.add}>添加</Button>
</View>
</View>
)
}
}

快速开发微信小程序

Taro 立足于微信小程序开发,众所周知小程序的开发体验并不是非常友好,比如小程序中无法使用 npm 来进行第三方库的管理,无法使用一些比较新的 ES 规范等等,针对小程序端的开发弊端,Taro 具有以下的优秀特性:

✅ 支持使用 npm/yarn 安装管理第三方依赖。

✅ 支持使用 ES7/ES8 甚至更加新的 ES 规范,一切都可自行配置。

✅ 支持使用 CSS 预编译器,例如 Sass 等。

✅ 支持使用 Redux 进行状态管理。

✅ 支持使用 Mobx 进行状态管理。

✅ 小程序 API 优化,异步 API Promise 化等等。

支持多端开发转化

Taro 方案的初心就是为了打造一个多端开发的解决方案。目前 Taro 代码可以支持转换到 微信/百度/支付宝小程序 、 H5 端 以及 移动端(React-Native)。

  1. 初始化环境