新项目启动
1 2 3 4
   |  npm i -g @tarojs/cli
  taro init appName
 
  | 
 
微信小程序
需要下载微信小程序开发工具,使用工具打开 taro 项目根目录。
老项目更新依赖
老项目在新 taro 框架,需要更新
Taro3 和老版本引入有差别
引入
1 2 3 4 5 6
   |  import Taro, { Component } from "@tarojs/taro";
 
  import Taro from "@tarojs/taro"; import { Component } from "react";
 
  | 
 
挂载 App.js
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
   |  import Taro, { Component } from "@tarojs/taro"; import Index from "./pages/index"; import "./app.css";
  class App extends Component {         config = {     pages: [       "pages/index/index",       "pages/nodes/nodes",       "pages/hot/hot",       "pages/node_detail/node_detail",       "pages/thread_detail/thread_detail",     ],     tabBar: {       list: [         {           iconPath: "resource/latest.png",           selectedIconPath: "resource/lastest_on.png",           pagePath: "pages/index/index",           text: "最新",         },         {           iconPath: "resource/hotest.png",           selectedIconPath: "resource/hotest_on.png",           pagePath: "pages/hot/hot",           text: "热门",         },         {           iconPath: "resource/node.png",           selectedIconPath: "resource/node_on.png",           pagePath: "pages/nodes/nodes",           text: "节点",         },       ],       color: "#000",       selectedColor: "#56abe4",       backgroundColor: "#fff",       borderStyle: "white",     },     window: {       backgroundTextStyle: "light",       navigationBarBackgroundColor: "#fff",       navigationBarTitleText: "V2EX",       navigationBarTextStyle: "black",     },   };         render() {     return (       <Index />            );   } }
 
 
  Taro.render(<App />, document.getElementById("app"));
 
 
  | 
 
按需引入
taro-UI  的样式可以按需引入.
研究多 tab 页报错问题,路径地址写错了
路由
跳转
小程序的跳转Taro.navigateTo.
H5 的跳转是从@tarojs/router中引入navigateTo.
路由声明
路由在 @tarojs/taro  中的 getCurrentInstance().router 中获取.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | import { getCurrentInstance } from "@tarojs/taro"; class C extends Component {   current = getCurrentInstance();
    componentWillMount() {          console.log(this.current.router);   } }
 
  import { getCurrentInstance } from "@tarojs/taro"; function C() {   const { router } = getCurrentInstance();       }
  | 
 
路由参数获取
原生小程序路由一般在 onLoad  中获取传过来的参数.
Taro  推荐在 ComponentDidShow  中获取.(也可以在 onLoad  中)
1 2 3 4 5 6 7 8 9
   |  class App extends Component {   componentDidShow (options ) {   }
    render () {     ...   } }
 
  | 
 
生命周期
小程序 => class 类 => 函数式 hook
onLoad => onLoad
onReady => onReady => useReady
onLanch => onLanch
onShow => conponentDidShow => useDidShow
onHide =>componentDidHide => useDidHide
事件
onPullDownRefresh()
监听用户下拉动作。
需要在全局配置的  window  选项中或页面配置中设置  enablePullDownRefresh: true。
可以通过  Taro.startPullDownRefresh  触发下拉刷新,调用后触发下拉刷新动画,效果与用户手动下拉刷新一致。
当处理完数据刷新后,Taro.stopPullDownRefresh  可以停止当前页面的下拉刷新.
onReachBottom()
监听用户上拉触底事件。
可以在全局配置的  window  选项中或页面配置中设置触发距离  onReachBottomDistance。
在触发距离内滑动期间,本事件只会被触发一次
H5  暂时没有同步实现,可以通过给  window  绑定  scroll  事件来进行模拟
onPageScroll()
监听用户滑动页面事件.
H5  暂时没有同步实现,可以通过给  window  绑定  scroll  事件来进行模拟
基本事件
在  Taro  中事件遵从小驼峰式(camelCase)命名规范,所有内置事件名以  on  开头。
在事件回调函数中,第一个参数是事件本身,回调中调用  stopPropagation  可以阻止冒泡。
只有小程序的  bindtap  对应  Taro  的  onClick
其余小程序事件名把  bind  换成  on  即是  Taro  事件名
Redux
安装 redux 及其中间件
1 2 3
   | $ yarn add redux react-redux redux-thunk redux-logger
  $ npm install --save redux react-redux redux-thunk redux-logger
   | 
 
随后可以在项目  src  目录下新增一个  store  目录,在目录下增加  index.js  文件用来配置  store,按自己喜好设置  redux  的中间件,例如下面例子中使用  redux-thunk  和  redux-logger  这两个中间件
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
   | import { createStore, applyMiddleware, compose } from "redux"; import thunkMiddleware from "redux-thunk"; import rootReducer from "../reducers";
  const composeEnhancers =   typeof window === "object" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__     ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({                })     : compose;
  const middlewares = [thunkMiddleware];
  if (   process.env.NODE_ENV === "development" &&   process.env.TARO_ENV !== "quickapp" ) {   middlewares.push(require("redux-logger").createLogger()); }
  const enhancer = composeEnhancers(   applyMiddleware(...middlewares)    );
  export default function configStore() {   const store = createStore(rootReducer, enhancer);   return store; }
  | 
 
接下来在项目入口文件  app.js  中使用  redux  中提供的  Provider  组件将前面写好的  store  接入应用中.
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
   | import React, { Component } from "react";
 
  import { Provider } from "react-redux"; import configStore from "./store";
 
  import "./app.css";
  const store = configStore();
  class App extends Component {      componentDidMount() {}
       onLaunch() {}
       componentDidShow() {}
       componentDidHide() {}
    render() {          return (       <Provider store={store}>         /* this.props.children 是将要被渲染的页面 */         {this.props.children}       </Provider>     );   } }
  export default App;
  | 
 
然后就可以开始使用了。如  redux  推荐的那样,可以增加
- constants  目录,用来放置所有的  action type  常量
 
- actions  目录,用来放置所有的  actions
 
- reducers  目录,用来放置所有的  reducers