taro框架的缺陷以及注意事項


1.不要使用 eval()

2.

禁止使用 Object 構造器

let config = new Object() // ✗ 錯誤

 

3.

不使用 Generator 函數語法

使用 Promise 或者 async functions 來實現異步編程

  1.  
    function* helloWorldGenerator() { // ✗ 錯誤
  2.  
    yield 'hello';
  3.  
    yield 'world';
  4.  
    return 'ending';
  5.  
    }

4.

SX 屬性均使用單引號

  1.  
    import Taro, { Component } from '@tarojs/taro'
  2.  
    import { View, Input } from '@tarojs/components'
  3.  
     
  4.  
    class MyComponent extends Component {
  5.  
    render () {
  6.  
    return (
  7.  
    < View className='test'> // ✓ 正確
  8.  
    < Text className="test_text">12</Text> // ✗ 錯誤
  9.  
    </ View>
  10.  
    )
  11.  
    }
  12.  
    }

5.

推薦使用對象解構的方式來使用 state、props

  1.  
    import Taro, { Component } from '@tarojs/taro'
  2.  
    import { View, Input } from '@tarojs/components'
  3.  
     
  4.  
    class MyComponent extends Component {
  5.  
    state = {
  6.  
    myTime: 12
  7.  
    }
  8.  
    render () {
  9.  
    const { isEnable } = this.props // ✓ 正確
  10.  
    const { myTime } = this.state // ✓ 正確
  11.  
    return (
  12.  
    < View className='test'>
  13.  
    {isEnable && < Text className='test_text'>{myTime}</Text>}
  14.  
    </ View>
  15.  
    )
  16.  
    }
  17.  
    }

 

6.

不要以 class/id/style 作為自定義組件的屬性名

  1.  
    <Hello class='foo' /> // ✗ 錯誤
  2.  
    <Hello id='foo' /> // ✗ 錯誤
  3.  
    <Hello style='foo' /> // ✗ 錯誤

7.

不要在調用 this.setState 時使用 this.state

由於 this.setState 異步的緣故,這樣的做法會導致一些錯誤,可以通過給 this.setState 傳入函數來避免

  1.  
    this.setState({
  2.  
    value: this.state.value + 1
  3.  
    }) // ✗ 錯誤
  4.  
     
  5.  
     
  6.  
    this.setState(prevState => ({ value: prevState.value + 1 })) // ✓ 正確

8.

map 循環時請給元素加上 key 屬性

  1.  
    list.map( item => {
  2.  
    return (
  3.  
    <View className='list_item' key={item.id}>{item.name}</View>
  4.  
    )
  5.  
    })

9.

盡量避免在 componentDidMount 中調用 this.setState

因為在 componentDidMount 中調用 this.setState 會導致觸發更新

  1.  
    import Taro, { Component } from '@tarojs/taro'
  2.  
    import { View, Input } from '@tarojs/components'
  3.  
     
  4.  
    class MyComponent extends Component {
  5.  
    state = {
  6.  
    myTime: 12
  7.  
    }
  8.  
     
  9.  
    componentDidMount () {
  10.  
    this.setState({ // ✗ 盡量避免,可以在 componentWillMount 中處理
  11.  
    name: 1
  12.  
    })
  13.  
    }
  14.  
     
  15.  
    render () {
  16.  
    const { isEnable } = this.props
  17.  
    const { myTime } = this.state
  18.  
    return (
  19.  
    < View className='test'>
  20.  
    {isEnable && < Text className='test_text'>{myTime}</Text>}
  21.  
    </ View>
  22.  
    )
  23.  
    }
  24.  
    }

10.

不要在 componentWillUpdate/componentDidUpdate/render 中調用 this.setState

  1.  
    import Taro, { Component } from '@tarojs/taro'
  2.  
    import { View, Input } from '@tarojs/components'
  3.  
     
  4.  
    class MyComponent extends Component {
  5.  
    state = {
  6.  
    myTime: 12
  7.  
    }
  8.  
     
  9.  
    componentWillUpdate () {
  10.  
    this.setState({ // ✗ 錯誤
  11.  
    name: 1
  12.  
    })
  13.  
    }
  14.  
     
  15.  
    componentDidUpdate () {
  16.  
    this.setState({ // ✗ 錯誤
  17.  
    name: 1
  18.  
    })
  19.  
    }
  20.  
     
  21.  
    render () {
  22.  
    const { isEnable } = this.props
  23.  
    const { myTime } = this.state
  24.  
    this.setState({ // ✗ 錯誤
  25.  
    name: 11
  26.  
    })
  27.  
    return (
  28.  
    < View className='test'>
  29.  
    {isEnable && < Text className='test_text'>{myTime}</Text>}
  30.  
    </ View>
  31.  
    )
  32.  
    }
  33.  
    }

不要定義沒有用到的 state

 

11.

組件最好定義 defaultProps

  1.  
    import Taro, { Component } from '@tarojs/taro'
  2.  
    import { View, Input } from '@tarojs/components'
  3.  
     
  4.  
    class MyComponent extends Component {
  5.  
     
  6.  
    static defaultProps = {
  7.  
    isEnable: true
  8.  
    }
  9.  
     
  10.  
    state = {
  11.  
    myTime: 12
  12.  
    }
  13.  
     
  14.  
    render () {
  15.  
    const { isEnable } = this.props
  16.  
    const { myTime } = this.state
  17.  
     
  18.  
    return (
  19.  
    <View className='test'>
  20.  
    {isEnable && <Text className='test_text'>{myTime}</Text>}
  21.  
    </View>
  22.  
    )
  23.  
    }
  24.  
    }

12.taro注意:

不能在包含 JSX 元素的 map 循環中使用 if 表達式

以下代碼會被 ESLint 提示警告,同時在 Taro(小程序端)也不會有效:

  1.  
    numbers.map( (number) => {
  2.  
    let element = null
  3.  
    const isOdd = number % 2
  4.  
    if (isOdd) {
  5.  
    element = <Custom />
  6.  
    }
  7.  
    return element
  8.  
    })

以下代碼不會被警告,也應當在 Taro 任意端中能夠運行:

  1.  
    numbers.map( (number) => {
  2.  
    let isOdd = false
  3.  
    if (number % 2) {
  4.  
    isOdd = true
  5.  
    }
  6.  
    return isOdd && <Custom />
  7.  
    })

 

13.

盡量在 map 循環中使用條件表達式或邏輯表達式

  1.  
    numbers.map( (number) => {
  2.  
    const isOdd = number % 2
  3.  
    return isOdd ? <Custom /> : null
  4.  
    })
  5.  
     
  6.  
    numbers.map( (number) => {
  7.  
    const isOdd = number % 2
  8.  
    return isOdd && <Custom />
  9.  
    })

 

14不能使用 Array#map 之外的方法操作 JSX 數組

15.

先處理好需要遍歷的數組,然后再用處理好的數組調用 map 方法。

  1.  
    numbers.filter(isOdd).map( (number) => <View />)
  2.  
     
  3.  
    for (let index = 0; index < array.length; index++) {
  4.  
    // do you thing with array
  5.  
    }
  6.  
     
  7.  
    const element = array.map(item => {
  8.  
    return <View />
  9.  
    })

 

16.

不能在 JSX 參數中使用匿名函數

以下代碼會被 ESLint 提示警告,同時在 Taro(小程序端)也不會有效:

  1.  
    <View onClick={ () => this.handleClick()} />
  2.  
     
  3.  
    <View onClick={ (e) => this.handleClick(e)} />
  4.  
     
  5.  
    <View onClick={ () => ({})} />
  6.  
     
  7.  
    <View onClick={ function () {}} />
  8.  
     
  9.  
    <View onClick={ function (e) {this.handleClick(e)}} />

 

17.

以下代碼不會被警告,也應當在 Taro 任意端中能夠運行:

  1.  
    <View onClick={ this.hanldeClick} />
  2.  
     
  3.  
    <View onClick={ this.props.hanldeClick} />
  4.  
     
  5.  
    <View onClick={ this.hanldeClick.bind(this)} />
  6.  
     
  7.  
    <View onClick={ this.props.hanldeClick.bind(this)} />

 

18

不能在 JSX 參數中使用對象展開符

以下代碼會被 ESLint 提示警告,同時在 Taro(小程序端)也不會有效:

  1.  
    <View {...this.props} />
  2.  
     
  3.  
    <View {...props} />
  4.  
     
  5.  
    <Custom {...props} />

 

19

不支持無狀態組件

以下代碼會被 ESLint 提示警告,同時在 Taro(小程序端)也不會有效:

  1.  
    function Test () {
  2.  
    return <View />
  3.  
    }
  4.  
     
  5.  
    function Test (ary) {
  6.  
    return ary.map(() => <View />)
  7.  
    }
  8.  
     
  9.  
    const Test = () => {
  10.  
    return <View />
  11.  
    }
  12.  
     
  13.  
    const Test = function () {
  14.  
    return <View />
  15.  
    }

 

20

微信小程序中 onLaunch 通常帶有一個參數 options,在 Taro 中你可以在所有生命周期和普通事件方法中通過 this.$router.params 訪問到,在其他端也適用

21 render中不要寫邏輯

22.

路由傳參

我們可以通過在所有跳轉的 url 后面添加查詢字符串參數進行跳轉傳參,例如

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

這樣的話,在跳轉成功的目標頁的生命周期方法里就能通過 this.$router.params 獲取到傳入的參數,例如上述跳轉,在目標頁的 componentWillMount 生命周期里獲取入參

  1.  
    class C extends Taro.Component {
  2.  
    componentWillMount () {
  3.  
    console.log( this.$router.params) // 輸出 { id: 2, type: 'test' }
  4.  
    }
  5.  
    }

23

在 Taro 中尺寸單位建議使用 px、 百分比 %,Taro 默認會對所有單位進行轉換。在 Taro 中書寫尺寸按照 1:1 的關系來進行書寫,即從設計稿上量的長度 100px,那么尺寸書寫就是 100px,當轉成微信小程序的時候,尺寸將默認轉換為 100rpx,當轉成 H5 時將默認轉換為以 rem 為單位的值。

如果你希望部分 px 單位不被轉換成 rpx 或者 rem ,最簡單的做法就是在 px 單位中增加一個大寫字母,例如 Px 或者 PX 這樣,則會被轉換插件忽略。

結合過往的開發經驗,Taro 默認以 750px 作為換算尺寸標准,如果設計稿不是以 750px 為標准,則需要在項目配置 config/index.js 中進行設置,例如設計稿尺寸是 640px,則需要修改項目配置 config/index.js 中的 designWidth配置為 640

  1.  
    const config = {
  2.  
    projectName: 'myProject',
  3.  
    date: '2018-4-18',
  4.  
    designWidth: 640,
  5.  
    ....
  6.  
    }

目前 Taro 支持 750、 640 、 828 三種尺寸設計稿,他們的換算規則如下:

  1.  
    const DEVICE_RATIO = {
  2.  
    '640': 2.34 / 2,
  3.  
    '750': 1,
  4.  
    '828': 1.81 / 2
  5.  
    }

24.

小程序樣式中引用本地資源

在小程序的樣式中,默認不能直接引用本地資源,只能通過網絡地址、Base64 的方式來進行資源引用,為了方便開發,Taro 提供了直接在樣式文件中引用本地資源的方式,其原理是通過 PostCSS 的 postcss-url 插件將樣式中本地資源引用轉換成 Base64 格式,從而能正常加載。

Taro 默認會對 10kb 大小以下的資源進行轉換,如果需要修改配置,可以在 config/index.js 中進行修改,配置位於 weapp.module.postcss

具體配置如下

  1.  
    // 小程序端樣式引用本地資源內聯
  2.  
    url: {
  3.  
    enable: true,
  4.  
    config: {
  5.  
    limit: 10240 // 設定轉換尺寸上限
  6.  
    }
  7.  
    }

25

JavaScript 表達式也可以嵌套:

  1.  
    render () {
  2.  
    const todos = ['finish doc', 'submit pr', 'nag dan to review'];
  3.  
    return (
  4.  
    <ul>
  5.  
    {todos.map((todo) => <Text>{todo}</Text>)}
  6.  
    </ul>
  7.  
    )
  8.  
    }

26 條件判斷

<View> {showHeader && <Header />} <Content /> </View> 

27

使用 PropTypes 檢查類型

隨着應用日漸龐大,你可以通過類型檢查捕獲大量錯誤。要檢查組件的屬性,你需要配置特殊的 propTypes 屬性:

  1.  
    import PropTypes from 'prop-types';
  2.  
     
  3.  
    class Greeting extends Component {
  4.  
    render() {
  5.  
    return (
  6.  
    <h1> Hello, {this.props.name}</h1>
  7.  
    );
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    Greeting.propTypes = {
  12.  
    name: PropTypes.string
  13.  
    };

如上例,Taro 與 React 一樣,也支持PropTypes 檢查類型,目前在小程序端還有些問題,但在 H5 端可以使用,用法和在 React 里一樣。 更多可參照React 的相關文檔

28

setState異步

  1.  
    // 假設我們之前設置了 this.state.counter = 0
  2.  
    updateCounter () {
  3.  
    this.setState({
  4.  
    counter: 1
  5.  
    }, () => {
  6.  
    // 在這個函數內你可以拿到 setState 之后的值
  7.  
    })
  8.  
    }

 

 

29

在 Taro 中另一個不同是你不能使用 catchEvent 的方式阻止事件冒泡。你必須明確的使用 stopPropagation。例如,阻止事件冒泡你可以這樣寫:

  1.  
    class Toggle extends React.Component {
  2.  
    constructor (props) {
  3.  
    super(props)
  4.  
    this.state = {isToggleOn: true}
  5.  
    }
  6.  
     
  7.  
    onClick = (e) => {
  8.  
    e.stopPropagation()
  9.  
    this.setState(prevState => ({
  10.  
    isToggleOn: !prevState.isToggleOn
  11.  
    }))
  12.  
    }
  13.  
     
  14.  
    render () {
  15.  
    return (
  16.  
    <button onClick={ this.onClick}>
  17.  
    { this.state.isToggleOn ? 'ON' : 'OFF'}
  18.  
    </button>
  19.  
    )
  20.  
    }
  21.  
    }

30

向事件處理程序傳遞參數

通常我們會為事件處理程序傳遞額外的參數。例如,若是 id 是你要刪除那一行的 id,以下兩種方式都可以向事件處理程序傳遞參數:

<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

31

當你通過 bind 方式向監聽函數傳參,在類組件中定義的監聽函數,事件對象 e 要排在所傳遞參數的后面。

  1.  
    class Popper extends Component {
  2.  
    constructor () {
  3.  
    super(...arguments)
  4.  
    this.state = { name:'Hello world!' }
  5.  
    }
  6.  
     
  7.  
    // 你可以通過 bind 傳入多個參數
  8.  
    preventPop (name, test, e) { //事件對象 e 要放在最后
  9.  
    e.preventDefault()
  10.  
    }
  11.  
     
  12.  
    render () {
  13.  
    return <Button onClick={this.preventPop.bind(this, this.state.name, 'test')}></Button>
  14.  
    }
  15.  
    }

32

父組件控制狀態值來決定子組件的條件渲染

33

Keys

但是在上面的代碼,你會得到一個報錯:提醒你當循環一個數組時應該提供 keys。Keys 可以在 DOM 中的某些元素被增加或刪除的時候幫助 Nerv/小程序 識別哪些元素發生了變化。因此你應當給數組中的每一個元素賦予一個確定的標識。

  1.  
    const numbers = [...Array(100).keys()] // [0, 1, 2, ..., 98, 99]
  2.  
    const listItems = numbers.map((number) => {
  3.  
    return <Text
  4.  
    key={ String(number)}
  5.  
    class='li'
  6.  
    >
  7.  
    我是第 { number + 1} 個數字
  8.  
    </Text>
  9.  
    })

34不同數組中可以使用相同的key

數組元素中使用的 key 在其兄弟之間應該是獨一無二的。然而,它們不需要是全局唯一的。當我們生成兩個不同的數組時,我們可以使用相同的 key:

  1.  
    class App extends Componenet {
  2.  
    state = {
  3.  
    posts: [
  4.  
    { id: 1, title: 'Hello World', content: 'Welcome to learning Taro!'},
  5.  
    { id: 2, title: 'Installation', content: 'You can install Taro from npm.'}
  6.  
    ]
  7.  
    }
  8.  
    render () {
  9.  
    const { posts } = this.state
  10.  
    const sidebar = (
  11.  
    <View>
  12.  
    {posts.map((post) =>
  13.  
    <Text key={post.id}>
  14.  
    {post.title}
  15.  
    </Text>
  16.  
    )}
  17.  
    </View>
  18.  
    )
  19.  
    const content = posts.map((post) => {
  20.  
    return <View key={post.id}>
  21.  
    <Text>{post.title}</Text>
  22.  
    <Text>{post.content}</Text>
  23.  
    </View>
  24.  
    })
  25.  
    return (
  26.  
    <View>
  27.  
    {sidebar}
  28.  
    <View class="divider" />
  29.  
    {content}
  30.  
    </View>
  31.  
    )
  32.  
    }
  33.  
    }

35

Children

在我們設計組件時,有些組件通常不知道自己的子組件會有什么內容,例如 Sidebar 和 Dialog 這樣的容器組件。

我們建議在這樣的情況使用 this.props.children 來傳遞子元素:

  1.  
    class Dialog extends Component {
  2.  
    render () {
  3.  
    return (
  4.  
    < View className='dialog'>
  5.  
    < View className='header'>Welcome!</View>
  6.  
    < View className='body'>
  7.  
    { this.props.children}
  8.  
    </ View>
  9.  
    < View className='footer'>-- divider --</View>
  10.  
    </ View>
  11.  
    )
  12.  
    }
  13.  
    }

這樣就能允許其它組件在 JSX 中嵌套任意子組件傳遞給 Dialog:

  1.  
    class App extends Component {
  2.  
    render () {
  3.  
    return (
  4.  
    < View className='container'>
  5.  
    < Dialog>
  6.  
    < View className="dialog-message">
  7.  
    Thank you for using Taro.
  8.  
    </ View>
  9.  
    </ Dialog>
  10.  
    </ View>
  11.  
    )
  12.  
    }
  13.  
    }

在 <Dialog /> JSX 標簽內的任何內容都會作為它的子元素(Children)都會傳遞到它的組件。

36  refs   rechart 元素綁定初始化

class MyComponent extends Component { componentDidMount () { // 如果 ref 的是小程序原生組件,那只有在 didMount 生命周期之后才能通過 // this.refs.input 訪問到小程序原生組件 if (process.env.TARO_ENV === 'weapp') { // 這里 this.refs.input 訪問的時候通過 `wx.createSeletorQuery` 取到的小程序原生組件 } else if (process.env.TARO_ENV === 'h5') { // 這里 this.refs.input 訪問到的是 `@tarojs/components` 的 `Input` 組件實例 } } render () { return <Input ref='input' /> } } 

37

利用 externalClasses 定義段定義若干個外部樣式類。這個特性從小程序基礎庫版本 1.9.90 開始支持。

  1.  
    /* CustomComp.js */
  2.  
    export default CustomComp extends Component {
  3.  
    static externalClasses = ['my-class']
  4.  
     
  5.  
    render () {
  6.  
    return <View className="my-class">這段文本的顏色由組件外的 class 決定</View>
  7.  
    }
  8.  
    }
  1.  
    /* MyPage.js */
  2.  
    export default MyPage extends Component {
  3.  
    render () {
  4.  
    return <CustomComp my-class="red-text" />
  5.  
    }
  6.  
    }
  1.  
    /* MyPage.scss */
  2.  
    .red-text {
  3.  
    color: red;
  4.  
    }

38

全局樣式類

使用外部樣式類可以讓組件使用指定的組件外樣式類,如果希望組件外樣式類能夠完全影響組件內部,可以將組件構造器中的 options.addGlobalClass 字段置為 true。這個特性從小程序基礎庫版本 2.2.3 開始支持。

  1.  
    /* CustomComp.js */
  2.  
    export default CustomComp extends Component {
  3.  
    static options = {
  4.  
    addGlobalClass: true
  5.  
    }
  6.  
     
  7.  
    render () {
  8.  
    return <View className="red-text">這段文本的顏色由組件外的 class 決定</View>
  9.  
    }
  10.  
    }
  1.  
    /* 組件外的樣式定義 */
  2.  
    .red-text {
  3.  
    color: red;
  4.  
    }

 

39

使用 this.$componentType 來判斷當前 Taro.Component 是頁面還是組件

40

this.$componentType 可能取值分別為 PAGE 和 COMPONENT,開發者可以根據此變量的取值分別采取不同邏輯。

 

41

在微信小程序中,從調用 Taro.navigateToTaro.redirectTo 或 Taro.switchTab 后,到頁面觸發 componentWillMount 會有一定延時。因此一些網絡請求可以提前到發起跳轉前一刻去請求。

Taro 提供了 componentWillPreload 鈎子,它接收頁面跳轉的參數作為參數。可以把需要預加載的內容通過 return 返回,然后在頁面觸發 componentWillMount 后即可通過 this.$preloadData 獲取到預加載的內容。

  1.  
    class Index extends Component {
  2.  
    componentWillMount () {
  3.  
    console.log( 'isFetching: ', this.isFetching)
  4.  
    this.$preloadData
  5.  
    .then(res => {
  6.  
    console.log( 'res: ', res)
  7.  
    this.isFetching = false
  8.  
    })
  9.  
    }
  10.  
     
  11.  
    componentWillPreload (params) {
  12.  
    return this.fetchData(params.url)
  13.  
    }
  14.  
     
  15.  
    fetchData () {
  16.  
    this.isFetching = true
  17.  
    ...
  18.  
    }
  19.  
    }

 

42

預加載

在微信小程序中,從調用 Taro.navigateToTaro.redirectTo 或 Taro.switchTab 后,到頁面觸發 componentWillMount 會有一定延時。因此一些網絡請求可以提前到發起跳轉前一刻去請求。

Taro 提供了 componentWillPreload 鈎子,它接收頁面跳轉的參數作為參數。可以把需要預加載的內容通過 return 返回,然后在頁面觸發 componentWillMount 后即可通過 this.$preloadData 獲取到預加載的內容。

  1.  
    class Index extends Component {
  2.  
    componentWillMount () {
  3.  
    console.log( 'isFetching: ', this.isFetching)
  4.  
    this.$preloadData
  5.  
    .then(res => {
  6.  
    console.log( 'res: ', res)
  7.  
    this.isFetching = false
  8.  
    })
  9.  
    }
  10.  
     
  11.  
    componentWillPreload (params) {
  12.  
    return this.fetchData(params.url)
  13.  
    }
  14.  
     
  15.  
    fetchData () {
  16.  
    this.isFetching = true
  17.  
    ...
  18.  
    }
  19.  
    }

 

 

43 全局變量

全局變量

在 Taro 中推薦使用 Redux 來進行全局變量的管理,但是對於一些小型的應用, Redux 就可能顯得比較重了,這時候如果想使用全局變量,推薦如下使用。

新增一個自行命名的 JS 文件,例如 global_data.js,示例代碼如下

  1.  
    const globalData = {}
  2.  
     
  3.  
    export function set (key, val) {
  4.  
    globalData[key] = val
  5.  
    }
  6.  
     
  7.  
    export function get (key) {
  8.  
    return globalData[key]
  9.  
    }

隨后就可以在任意位置進行使用啦

  1.  
    import { set as setGlobalData, get as getGlobalData } from './path/name/global_data'
  2.  
     
  3.  
    setGlobalData( 'test', 1)
  4.  
     
  5.  
    getGlobalData( 'test')

44 上傳下載

上傳、下載

Taro.uploadFile(OBJECT)

使用方式同 wx.uploadFile,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    const uploadTask = Taro.uploadFile(params).then(...)

Taro.downloadFile(OBJECT)

使用方式同 wx.downloadFile,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.downloadFile(params).then(...)

 

45 端能力api

圖片

Taro.chooseImage(OBJECT)

使用方式同 wx.chooseImage,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.chooseImage(params).then(...)

Taro.previewImage(OBJECT)

使用方式同 wx.previewImage,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.previewImage(params).then(...)

Taro.getImageInfo(OBJECT)

使用方式同 wx.getImageInfo,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.getImageInfo(params).then(...)

Taro.saveImageToPhotosAlbum(OBJECT)

使用方式同 wx.saveImageToPhotosAlbum,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.saveImageToPhotosAlbum(params).then(...)
 
 

46 端能力api

Taro.showToast(OBJECT)

Taro.showToast({ title: '成功', icon: 'success', duration: 2000 }) .then(res => console.log(res))

Taro.showLoading(OBJECT)

顯示 loading 提示框, 需主動調用 Taro.hideLoading 才能關閉提示框,支持 Promise 化使用。

OBJECT 參數說明:

參數 類型 必填 說明
title String 提示的內容
mask Boolean 是否顯示透明蒙層,防止觸摸穿透,默認:false
success Function 接口調用成功的回調函數
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.showLoading({
  4.  
    title: 'loading'
  5.  
    })
  6.  
    .then( res => console.log(res))

Taro.hideToast()

隱藏消息提示框

Taro.hideLoading()

隱藏 loading 提示框

Taro.showModal(OBJECT)

import Taro from '@tarojs/taro' // 注意:無論用戶點擊確定還是取消,Promise 都會 resolve。 Taro.showModal({ title: 'xxx', content: 'hello world', }) .then(res => console.log(res.confirm, res.cancel))

 

Taro.showActionSheet(OBJECT)

顯示操作菜單,支持 Promise 化使用。

 

設置導航條

Taro.setNavigationBarTitle(OBJECT)

使用方式同 wx.setNavigationBarTitle,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.setNavigationBarTitle(params).then(...)

Taro.showNavigationBarLoading()

在當前頁面顯示導航條加載動畫。

Taro.hideNavigationBarLoading()

隱藏導航條加載動畫。

Taro.setNavigationBarColor(OBJECT)

使用方式同 wx.setNavigationBarColor,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.setNavigationBarColor(params).then(...)

API 支持度

API 微信小程序 H5 ReactNative
Taro.setNavigationBarTitle ✔️   ✔️
Taro.showNavigationBarLoading ✔️   ✔️
Taro.hideNavigationBarLoading ✔️   ✔️
Taro.setNavigationBarColor ✔️   ✔️(不支持 animation 參數)

設置 tabBar

Taro.setTabBarBadge(OBJECT)

使用方式同 wx.setTabBarBadge,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.setTabBarBadge(params).then(...)

Taro.removeTabBarBadge(OBJECT)

使用方式同 wx.removeTabBarBadge,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.removeTabBarBadge(params).then(...)

Taro.showTabBarRedDot(OBJECT)

使用方式同 wx.showTabBarRedDot,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.showTabBarRedDot(params).then(...)

Taro.hideTabBarRedDot(OBJECT)

使用方式同 wx.hideTabBarRedDot,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.hideTabBarRedDot(params).then(...)

Taro.setTabBarStyle(OBJECT)

使用方式同 wx.setTabBarStyle,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.setTabBarStyle(params).then(...)

Taro.setTabBarItem(OBJECT)

使用方式同 wx.setTabBarItem,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.setTabBarItem(params).then(...)

Taro.showTabBar(OBJECT)

使用方式同 wx.showTabBar,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.showTabBar(params).then(...)

Taro.hideTabBar(OBJECT)

使用方式同 wx.hideTabBar,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.hideTabBar(params).then(...)

API 支持度

API 微信小程序 H5 ReactNative
Taro.setTabBarBadge ✔️    
Taro.removeTabBarBadge ✔️    
Taro.showTabBarRedDot ✔️    
Taro.hideTabBarRedDot ✔️    
Taro.setTabBarStyle ✔️    
Taro.setTabBarItem ✔️    
Taro.showTabBar ✔️    
Taro.hideTabBar ✔️    

設置置頂信息

Taro.setTopBarText(OBJECT)

使用方式同 wx.setTopBarText,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.setTopBarText(params).then(...)

API 支持度

API 微信小程序 H5 ReactNative
Taro.setTopBarText ✔️    

導航

Taro.navigateTo(OBJECT)

使用方式同 wx.navigateTo,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.navigateTo(params).then(...)

Taro.redirectTo(OBJECT)

使用方式同 wx.redirectTo,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro. redirectTo(params).then(...)

Taro.switchTab(OBJECT)

使用方式同 wx.switchTab,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.switchTab(params).then(...)

Taro.navigateBack(OBJECT)

使用方式同 wx.navigateBack

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.navigateBack({ delta: 2 })

Taro.reLaunch(OBJECT)

使用方式同 wx.reLaunch,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.reLaunch(params).then(...)

Taro.getCurrentPages(OBJECT)

使用方式同 getCurrentPages, 獲取當前的頁面棧,決定需要返回幾層。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.getCurrentPages().length

API 支持度

API 微信小程序 H5 ReactNative
Taro.navigateTo ✔️ ✔️ ✔️
Taro.redirectTo ✔️ ✔️ ✔️
Taro.switchTab ✔️   ✔️
Taro.navigateBack ✔️ ✔️ ✔️
Taro.reLaunch ✔️   ✔️  
Taro.getCurrentPages ✔️   ✔️

動畫

Taro.createAnimation(OBJECT)

使用方式同 wx.createAnimation

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    const animation = Taro.createAnimation({
  4.  
    transformOrigin: "50% 50%",
  5.  
    duration: 1000,
  6.  
    timingFunction: "ease",
  7.  
    delay: 0
  8.  
    })

API 支持度

API 微信小程序 H5 ReactNative
Taro.createAnimation ✔️    

位置

Taro.pageScrollTo(OBJECT)

使用方式同 wx.pageScrollTo,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.pageScrollTo(params).then(...)

API 支持度

API 微信小程序 H5 ReactNative
Taro.pageScrollTo ✔️    

繪圖

Taro.createCanvasContext(canvasId, this.$scope)

使用方式同 wx.createCanvasContext

Taro.createContext(不推薦使用)

創建並返回繪圖上下文。

Taro.drawCanvas(不推薦使用)

使用方式同 wx.drawCanvas

API 支持度

API 微信小程序 H5 ReactNative
Taro.createCanvasContext ✔️    
Taro.createContext ✔️    
Taro.drawCanvas ✔️    

下拉刷新

Taro.startPullDownRefresh(OBJECT)

使用方式同 wx.startPullDownRefresh,支持 Promise 化使用。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.startPullDownRefresh(params).then(...)

Taro.stopPullDownRefresh()

停止當前頁面下拉刷新。

示例代碼:

  1.  
    import Taro from '@tarojs/taro'
  2.  
     
  3.  
    Taro.stopPullDownRefresh()
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM