taro多端实践初探

栏目: IOS · Android · 发布时间: 5年前

内容简介:历史的发展,小程序风行一时,安卓/ios/H5/微信小程序/支付宝小程序/头条小程序,产品经理让你适配这么多,你的心情如何呢?然而总会有人给咱们造出合适的工具,解放生产力,一次编码,多端运行。开始探索之旅吧!安装 Taro 开发工具 @tarojs/cli使用 npm 或者 yarn 全局安装,或者直接使用npx

历史的发展,小程序风行一时,安卓/ios/H5/微信小程序/支付宝小程序/头条小程序,产品经理让你适配这么多,你的心情如何呢?然而总会有人给咱们造出合适的工具,解放生产力,一次编码,多端运行。开始探索之旅吧!

taro安装

安装 Taro 开发工具 @tarojs/cli

使用 npm 或者 yarn 全局安装,或者直接使用npx

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

使用命令创建模版

$ taro init multiportApp

taro多端实践初探 按照自己情况选择安装即可

启动

进入对应目录,执行命令启动。

npm run dev:h5

会出现启动成功的界面,如下

taro多端实践初探 自动就会打开浏览器,出现hello world界面,表示项目启动成功了!

todolist功能实现

添加数据

在pages/index/index.js文件中添加如下

constructor(props){
    super(props);
    this.state={
      val:'',
      todos:[
        {
          title:'吃饭',
          done:false
        },
        {
          title:'睡觉',
          done:false
        },
        {
          title:'coding',
          done:false
        }
      ]
    }
  }

渲染数据

render () {
    return (
      <View className='index'>
        <Text>Hello world!</Text>
        {
          this.state.todos.map((item,index)=>{
            return <View key={index}>{item.title}</View>
          })
        }
      </View>
    )
  }

列表渲染搞定。

taro多端实践初探

添加输入框和按钮

引入组件

import { View, Text,Input,Button } from '@tarojs/components'

render修改

render () {
    return (
      <View className='index'>
        <Text>Hello world!</Text>
        <Input value={this.state.val} onInput={this.handleInput}></Input>
        <Button onClick={this.handleClick}>添加</Button>
        {
          this.state.todos.map((item,index)=>{
            return <View key={index}>{item.title}</View>
          })
        }
      </View>
    )
  }

添加方法

handleInput=(e)=>{
    this.setState({
      val:e.detail.value
    })
}

handleClick=()=>{
    this.setState({
      todos:[...this.state.todos,{title:this.state.val,done:false}],
      val:''
    })
}

一个简单的todolist就算完成了,界面有点丑,继续优化!

taro多端实践初探

优化,引入UI库

安装taro-ui

官网

npm install --save taro-ui

简单配置

由于引用 node_modules 的模块,默认不会编译,所以需要额外给 H5 配置 esnextModules ,在 taro 项目的 config/index.js 中新增如下配置项:

h5: {
  esnextModules: ['taro-ui']
}

全局引入

在app.scss中引入

@import 'taro-ui/dist/style/index.scss'

在index.js中引入

import { AtList, AtListItem } from "taro-ui"

修改render

render () {
    return (
      <View className='index'>
        <Text>Hello world!</Text>
        <Input value={this.state.val} onInput={this.handleInput}></Input>
        <Button onClick={this.handleClick}>添加</Button>
        <AtList>
          {
            this.state.todos.map((item,index)=>{
              return <AtListItem key={index} title={item.title}></AtListItem>
            })
          }
        </AtList>
      </View>
    )
  }

taro多端实践初探

添加滑块开关,改变item状态

<AtListItem key={index} title={item.title} className={{'done':item.done}} isSwitch switchIsCheck={item.done} onSwitchChange={(e)=>this.handleChange(e,index)}></AtListItem>

增加一个isSwitch,switch切换事件,class。

增加事件

handleChange=(e,i)=>{
    console.log(e,i);
    const todos=[...this.state.todos];
    todos[i].done=e.detail.value;
    this.setState({
      todos
    })
  }

在同级目录下index.scss中增加样式

.done{
  color: red;
  text-decoration: line-through;
}

效果

h5效果

taro多端实践初探

微信小程序中的效果

taro多端实践初探 这就是这个框架的威力,感谢taro开发团队。)

最后在说一句,正在找工作,坐标北京,各位大佬有合适的机会推荐下哈!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数文明

数文明

涂子沛 / 中信出版集团 / 2018-9 / 88.00元

从量数、据数、普适记录、人脸识别、以图搜车,到雾计算、城市大脑、单粒度治理、无匿名社会、量子思维……作为中国研究大数据的权威专家,作者在《数文明》一书中,以大数据为核心元素,抽丝剥茧,深入地阐述了这个大数据时代的文明社会——一个全新的数文明时代。 将大数据与人类文明融合在一起,这本书提供给我们的不仅是一种全新的叙事结构,它还将突破你的认知边界和思维极限,给你提供一个应对这个世界的全新的认知方......一起来看看 《数文明》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具