1. 程式人生 > >迅速上手:使用taro構建微信小程式基礎教程

迅速上手:使用taro構建微信小程式基礎教程

前言

由於微信小程式在開發上不能安裝npm依賴,和開發流程上也飽受詬病;Taro 是由京東·凹凸實驗室(aotu.io)傾力打造的 多端開發解決方案,它的api基於react,在本篇文章中主要介紹了使用taro搭建微信小程式的一些步驟和一個簡單demo的實現。

安裝

先全域性安裝@tarojs/cli

$ npm install -g @tarojs/cli $ yarn global add @tarojs/cli

之後我們初始化一個名為myApp的專案:

$ taro init myApp

然後輸入你的配置:
demo

之後等待所有依賴安裝完畢。

開發

在命令列執行

$ npm run dev:weapp

taro將會進入微信小程式編譯預覽模式。我們開啟微信開發者工具,將專案匯入,會在預覽視窗中看到hello world。這時我們就可以進行開發啦~~

1.生命週期函式

生命週期方法 作用 說明
componentWillMount 程式被載入 對應微信小程式onLaunch
componentDidMount 程式被載入 對應微信小程式onLaunch,在componentWillMount之後執行
componentDidShow 程式展示出來 對應微信小程式onShow
componentDidHide 程式被隱藏 對應微信小程式onHide

不過當然也包含componentWillUnmoutcomponentWillReceiveProps等react原始生命週期函式,用來編寫自定義元件。

2.路由

在 Taro 中,路由功能是預設自帶的,不需要開發者進行額外的路由配置。

// 跳轉到目的頁面,開啟新頁面
Taro.navigateTo({ url: '/pages/page/path/name' }) // 跳轉到目的頁面,在當前頁面開啟 Taro.redirectTo({ url: '/pages/page/path/name' })

傳參

// 傳入引數 id=2&type=test
Taro.navigateTo({ url: '/pages/page/path/name?id=2&type=test' })

我們可以使用this.$router.params來獲取路由上的引數。

3.元件

Taro 以 微信小程式元件庫 為標準,結合 jsx 語法規範,定製了一套自己的元件庫規範。這部分可以自行去看文件。*值得注意的是,小程式中的寫法`bind這種事件都要改成以on`開頭。**

寫個demo

現在使用taro構建一個很簡單的demo;需要實現簡單的元件呼叫,路由跳轉傳參等功能。

demo

1.主頁

一個Swiper,一個列表元件:

// index.js
import Taro, { Component } from '@tarojs/taro' import { View, Swiper,SwiperItem, Image } from '@tarojs/components' import ListItem from '../../components/ListItem' import './index.less' import img0 from './img/img0.jpg' import img1 from './img/img1.jpg' import img2 from './img/img2.jpg' export default class Index extends Component { config = { navigationBarTitleText: '首頁' } skipToDetail(){ /* */ } render() { return ( <View className='index'> <Swiper indicatorDots autoplay> {[img0,img1,img2].map(img=>(<SwiperItem key={img}><Image src={img} /></SwiperItem>))} </Swiper> {listSet.map(item=>(<ListItem onClick={this.skipToDetail.bind(this)} description={item.description} title={item.title} key={item.title} />))} </View> ) } } const listSet=[ {title:'標題一',description:'描述一'}, {title:'標題二',description:'描述二'}, {title:'標題三',description:'描述三'}, ]

列表元件,注意這裡有個坑,就是不能直接呼叫函式props,會報一個警告,說是沒有找到onClick handler。查閱官方文件後,在issues 215中找到了答案,官方說是會在以後的版本中修復這個bug,目前先按下面程式碼寫。

import Taro, { Component } from '@tarojs/taro' import { View, Text } from '@tarojs/components' import './index.less' export default class ListItem extends Component { skipToDetail(){ /* 必須得這樣寫=。= */ this.props.onClick() } render() { const { title, description } = this.props return ( <View className='list-item' onClick={this.skipToDetail}> <View><Text>{title}</Text></View> <View><Text>{description}</Text></View> </View> ) } }

2.詳情頁跳轉

我們在入口檔案新增新的路由,指向詳情頁detail:
這裡需要注意先配置好pages,然後再寫這個元件,要不再編譯的時候會找不到這個頁

// app.js
 config = { pages: [ 'pages/index/index', 'pages/detail/index' ], ... }

建立詳情頁:

import Taro, { Component } from '@tarojs/taro' import { View } from '@tarojs/components' import './index.less' export default class Index extends Component { componentWillMount () { } config = { navigationBarTitleText: '詳情頁' } render () { const {title,description}=this.$router.params return ( <View> ... </View> ) } }

要求點選每個列表項,需要進行跳轉,並把當前的title和description傳到詳情頁。需要在首頁中的skipToDetail中補充以下內容:

skipToDetail({title,description}){ Taro.navigateTo({ url: `/pages/detail/index?title=${title}&description=${description}` }) }

並在render方法中將引數傳入這個函式中:

 render() { return ( <View className='index'> ... {listSet.map(item=>(<ListItem onClick={this.skipToDetail.bind(this,item)} description={item.description} title={item.title} key={item.title} />))} </View> ) }

參考文件

  1. taro官網文件
 

連結:http://www.imooc.com/article/44296