Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taro --- 永远的神 #19

Open
lizejun123 opened this issue Jun 4, 2021 · 0 comments
Open

Taro --- 永远的神 #19

lizejun123 opened this issue Jun 4, 2021 · 0 comments

Comments

@lizejun123
Copy link
Owner

安装及使用

安装#

Taro 项目基于 node,请确保已具备较新的 node 环境(>=12.0.0),推荐使用 node 版本管理工具 nvm 来管理 node,这样不仅可以很方便地切换 node 版本,而且全局安装时候也不用加 sudo 了。

CLI 工具安装#

首先,你需要使用 npm 或者 yarn 全局安装 @tarojs/cli,或者直接使用 npx:

使用 npm 安装 CLI

$ npm install -g @tarojs/cli

OR 使用 yarn 安装 CLI

$ yarn global add @tarojs/cli

OR 安装了 cnpm,使用 cnpm 安装 CLI

$ cnpm install -g @tarojs/cli

项目初始化#

使用命令创建模板项目:

$ taro init myApp

npm 5.2+ 也可在不全局安装的情况下使用 npx 创建模板项目:

$ npx @tarojs/cli init myApp

在创建完项目之后,Taro 会默认开始安装项目所需要的依赖,安装使用的工具按照 yarn > cnpm > npm 顺序进行检测。一般来说,依赖安装会比较顺利,但某些情况下可能会安装失败,这时候你可以在项目目录下自己使用安装命令进行安装:

进入项目根目录

$ cd myApp

使用 yarn 安装依赖

$ yarn

OR 使用 cnpm 安装依赖

$ cnpm install

OR 使用 npm 安装依赖

$ npm install

编译运行#

使用 Taro 的 build 命令可以把 Taro 代码编译成不同端的代码,然后在对应的开发工具中查看效果。

Taro 编译分为 dev 和 build 模式:

dev 模式(增加 --watch 参数) 将会监听文件修改。
build 模式(去掉 --watch 参数) 将不会监听文件修改,并会对代码进行压缩打包。
dev 模式生成的文件较大,设置环境变量 NODE_ENV 为 production 可以开启压缩,方便预览,但编译速度会下降。
微信小程序#
编译命令#

yarn

$ yarn dev:weapp
$ yarn build:weapp

npm script

$ npm run dev:weapp
$ npm run build:weapp

仅限全局安装

$ taro build --type weapp --watch
$ taro build --type weapp

npx 用户也可以使用

$ npx taro build --type weapp --watch
$ npx taro build --type weapp

watch 同时开启压缩

$ set NODE_ENV=production && taro build --type weapp --watch # Windows
$ NODE_ENV=production taro build --type weapp --watch # Mac

小程序开发者工具#

下载并打开微信开发者工具,然后选择项目根目录进行预览。

需要注意开发者工具的项目设置:

  • 需要关闭 ES6 转 ES5 功能,开启可能报错
  • 需要关闭上传代码时样式自动补全,开启可能报错
  • 需要关闭代码压缩上传,开启可能报错
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant