taro做项目html写在哪里,用Taro做个微信小程序Todo, 小白工作记录

微信小程序框架: Taro

做微信小程序的框架, 几个比较主流的:

前两者都是Vue风格的, Taro是React的.

背景:

一直做Android开发, 最近想把其他各种技术都撸一撸, 拓展一下视野.

之前练过微信小程序原生开发的例子, 基本上只知道个大概, 翻书马冬梅, 合书马什么梅?

所以这次用框架了, 觉得还是要有个输出, 这样印象深刻一些.

所以本文是从一个小白的角度, 手把手做练习的.

Taro程序环境

这部分参考:

要有node环境, 推荐用nvm来管理.

需要安装CLI工具:

1npm install -g @tarojs/cli

创建项目并运行

创建项目

创建模板项目:

1taro init myApp

在这个阶段会有一些问题要回答.

1

2

3

4* 请输入项目介绍! My awesome project blablabla.

* 是否需要使用 TypeScript ? Yes

* 请选择 CSS 预处理器(Sass/Less/Stylus) Sass

* 请选择模板 默认模板

完了之后会自动安装依赖:

1npm install

然后就成功啦!

运行

Taro需要运行不同的命令, 将Taro代码编译成不同端的代码, 然后在对应的开发工具中查看效果.

import这个项目就行了.

开发前微信开发者工具的项目设置要注意:

1

2

3需要设置关闭ES6转ES5功能, 开启可能报错.

需要设置关闭上传代码时样式自动补全, 开启可能报错.

需要设置关闭代码压缩上传, 开启可能报错.

这些默认都是关闭的.

微信小程序的编译预览及打包:

npm script

1

2$ npm run dev:weapp

$ npm run build:weapp

或者:

1

2$ taro build --type weapp --watch

$ taro build --type weapp

加上--watch会监听文件修改, 去掉会对代码进行压缩打包.

运行编译命令之后就可以在微信开发者工具中预览了. 是显示一个Hello World.

项目结构

因为创建项目的时候用了TypeScript, 所以项目结构是这样的:

1

2

3

4

5

6

7

8

9

10

11

12

13

14├── dist 编译结果目录

├── config 配置目录

| ├── dev.js 开发时配置

| ├── index.js 默认配置

| └── prod.js 打包时配置

├── src 源码目录

| ├── pages 页面文件目录

| | ├── index index页面目录

| | | ├── index.scss index页面样式

| | | └── index.tsx index页面逻辑

| ├── app.scss 项目总通用样式

| ├── app.tsx 项目入口文件

| └── index.html

└── package.json

如果需要创建组件, 可以在src下创建component目录来统一存放组件.

“Hello world!”就在index.tsx中.

IDE

关于IDE:

我发现用微信开发者工具是打不开这些后缀名为scss和tsx的文件的.

微信开发者工具只能用来预览.

后来用了Intellij, 是能查看文件了,

但是开始写代码之后, 发现没有自动提示, 也没有自动格式化工具.

又下了个VS CODE:

1brew cask install visual-studio-code

格式化的快捷键是:

Shift + Alt + F.

后来我在Text Editor设置里勾选了Format On Save.

编写代码

Step 1: 静态页面显示

先弄几个数据静态显示一下:

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

32export default class Index extends Component {

config: Config = {

navigationBarTitleText: '首页'

}

constructor(props) {

super(props)

this.state = {

list: ['get up', 'eat breakfast', 'study',]

}

inputVal: ''

}

render() {

let { list, inputVal } = this.state

return (

Todo List

{

list.map((item, index) => {

return

{index + 1}.{item}

})

}

)

}

}

然后命令行运行:

1taro build --type weapp --watch

就会直接显示出来结果. 之后的修改也是随时呈现.

这里IDE报告两个TypeScript的问题:

1Property 'list' does not exist on type 'Readonly'

和:

1Property 'inputVal' does not exist on type 'Index'.

按照这里的方法修改的:

1

2

3

4

5

6

7

8

9

10

11interface MyProps {

}

interface MyState {

list: Array;

inputVal: string;

}

export default class Index extends Component {

//...

}

代码中设置值的时候调用this.setState().

Step 2: 添加和删除项目

加上输入框, 添加和删除按钮的对应方法.

完整代码:

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

64

65

66

67

68

69

70interface MyProps {

}

interface MyState {

list: Array;

inputVal: string;

}

export default class Index extends Component {

config: Config = {

navigationBarTitleText: '首页'

}

constructor(props) {

super(props)

this.state = {

list: ['get up', 'eat breakfast', 'study',],

inputVal: ''

}

}

addItem() {

let { list } = this.state

const inputVal = this.state.inputVal

if (inputVal == '') return

else {

list.push(inputVal)

}

this.setState({

list,

inputVal: ''

})

}

removeItem(index) {

let { list } = this.state

list.splice(index, 1)

this.setState({

list

})

}

inputHandler(e) {

this.setState({ inputVal: e.target.value })

}

render() {

let { list, inputVal } = this.state

return (

添加

Todo List

{

list.map((item, index) => {

return

{index + 1}.{item}

删除

})

}

)

}

}

Step 3: 样式调整

然后企图调整button的对齐. (CSS不太擅长, 请轻拍, 请指教.)

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.input {

display: inline-block;

margin: 32px;

border: 1px solid #666;

width: 65%;

vertical-align: middle;

}

.list_wrap {

padding: 32px;

}

.list_item {

margin: 20px 0;

}

.add,

.del {

display: inline-block;

width: 120px;

height: 60px;

margin: 0 10px;

padding: 0 10px;

font-size: 22px;

line-height: 60px;

text-align: center;

border-radius: 10px;

box-sizing: border-box;

overflow: hidden;

text-overflow: ellipsis;

white-space: nowrap;

justify-content: center;

vertical-align: middle;

}

.add {

background-color: #5c89e4;

color: #fff;

border: 1px solid #5c89e4;

}

.del {

background-color: #fff;

color: #5c89e4;

border: 1px solid #5c89e4;

position: absolute;

right: 32px;

}

Taro命令

快速生成新的页面文件:

1Taro create --name [页面名称]

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值