Taro踩坑指南

作者: 晌风 | 来源:发表于2019-06-18 13:06 被阅读0次

jsx中不能使用匿名函数, 需要bind

<View onClick={this.handleClick}></View>  // 正确
<View onClick={()=>this.handleClick(params)}></View>  // 错误
<View onClick={this.handleClick.bind(this, params)}></View>  // 正确

Taro中的跳转事件与生命周期的关系

  • navigateTo 会进 componentDidhide

  • relaunch, redirect, navigateBack 以及物理返回 会进 componentWillUnmount

应用:如果要监听taro中的返回事件,则可以设置一个state,在所有非返回的页面跳转事件中改变state的值,然后在componentwillUnmount中判断state的值是否等于初始状态,如果等于则是返回事件,否则是其他的跳转。

不能在render函数中定义纯函数组件,否则打包为小程序的时候会报错

taro Cannot read property 'isCallExpression' of null

解决:去除render return之前定义的纯函数组件 使用class组件代替

文本溢出显示省略号样式不不生效

// 多行截取
@mixin textOrient($line) {
    display: -webkit-box;

    // 需要加上这一句autoprefixer的忽略规则 否则这一行样式加不上 导致无法展示省略号
    /*! autoprefixer: ignore next */
    -webkit-box-orient: vertical;

    -webkit-line-clamp: $line;
    overflow: hidden;
}

移动滑动不跟手的问题

page{    // 全局设置小程序的样式
    -webkit-overflow-scrolling: touch;
}
#app {   // 全局设置网页样式
    height: auto;
    -webkit-overflow-scrolling: touch;
}

错误图片处理

解决方案:每一个Image标签传入一个唯一key,使用onError事件监听,如果进了就设置对应的state为对应的图片路径,用传入的key来作为state的键以保证唯一性。
下面是封装的组件


import Taro, { Component } from '@tarojs/taro';
import { Image } from '@tarojs/components';
import defaultStoreIcon from './../assets/images/common/headimg.jpg'
import defaultGoodsIcon from './../assets/images/common/icon_default.png'
import defaultPersonIcon from './../assets/images/common/person_zhanweitu@2x.png'

/**
 * 组件需要的Props定义
 */
interface IProps {
  className: string;
  src: string;
  type: string;
  key: string;
}
/**
 * 分割线
 */

/**
 * api同taro Image 增加type和key属性 用于处理图片路径错误
 * @type 图片类型 String store-店铺 person-用户 goods-商品
 * @key 图片key值,需要保证页面唯一
 * @src 图片路径 同Image src
 * @className 图片类名 同Image className
 */

const iconEnum = {
  'store': defaultStoreIcon,
  'person': defaultPersonIcon,
  'goods': defaultGoodsIcon
}
 
export default class BackTop extends Component<IProps> {
  
    state = {};
  
    // 错误时间监听 设置唯一state
    handleError = key => {
        const { type } = this.props
        this.setState({
            [`imgError${key}`]: iconEnum[type] || defaultGoodsIcon  // 如果未传入type则显示默认的商品图片
        })
    }

    render() {
        const { className, src, type, key } = this.props
        return (
            <Image {...this.props}
                src={this.state[`imgError${key}`] || src}
                onError={this.handleError.bind(this, key)}
            />
        );
    }
}

持续踩坑,持续更新中...

相关文章

  • Taro踩坑指南

    jsx中不能使用匿名函数, 需要bind Taro中的跳转事件与生命周期的关系 navigateTo 会进 co...

  • JavaScrip-StepPitGuide《JavaScrip

    《JavaScript踩坑指南》JavaScrip-StepPitGuide? 《JavaScript踩坑指南》 ...

  • Taro踩坑

    小程序转Taro 小程序转Taro的时候,会带上一个@withWeapp('page'),这是一个标识,用来表示是...

  • Taro踩坑

    网络相关 1、请求接口没有响应解决方法:配置httpRequest接口请求域名白名单 使用build时候出现的错误...

  • taro踩坑

    编译h5的时候遇到以下错 解决方案如下config/index.js添加esnextModule:['taro-ui']

  • taro踩坑

    使用taro下的组建ScrollView 时候样式出不来 全局样式引入import 'taro-ui/dist/s...

  • 微信小程序开发 — 开发踩坑记录

    说明 本文记录了小程序项目开发过程中踩过的坑,方便日后参考。 问题 taro-ui 问题 公司项目采用 taro-...

  • Taro 实战踩坑

    Taro 引用相对路径图片 直接将相对路径放在src属性中,不起作用,需要先import进来,最好把图片放到服务器...

  • taro 踩坑ing

    最近使用 taro 来开发扩平台的电商项目。首先兼容的是小程序,使用的工具是微信开发者工具,H5 和 RN 在以后...

  • Taro 兼容 h5 踩坑指南

    最近一周在改造 公羊阅读? Taro 版本适配 h5 端,过程中改改补补,好不酸爽。本文记录?遇到的问题,希望为有...

网友评论

    本文标题:Taro踩坑指南

    本文链接:https://www.haomeiwen.com/subject/bnxfqctx.html