快速开始
Taro 快速开始
# 使用 npm 安装 CLI
$ npm install -g @tarojs/cli
# OR 使用 yarn 安装 CLI
$ yarn global add @tarojs/cli
# OR 安装了 cnpm,使用 cnpm 安装 CLI
$ cnpm install -g @tarojs/cli
值得一提的是,如果安装过程出现
$ npm install -g mirror-config-china
项目初始化
使用命令创建模板项目
$ taro init myApp
$ npx @tarojs/cli init myApp
在创建完项目之后,
# 使用 yarn 安装依赖
$ yarn
# OR 使用 cnpm 安装依赖
$ cnpm install
# OR 使用 npm 安装依赖
$ npm install
进入项目目录开始开发,目前已经支持 微信
运行

在使用
微信小程序
选择微信小程序模式,需要自行下载并打开微信开发者工具,然后选择项目根目录进行预览。微信小程序编译预览及打包(去掉 –
# 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
路由功能
在
// 跳转到目的页面,打开新页面
Taro.navigateTo({
url: "/pages/page/path/name"
});
// 跳转到目的页面,在当前页面打开
Taro.redirectTo({
url: "/pages/page/path/name"
});
路由传参
我们可以通过在所有跳转的 url
后面添加查询字符串参数进行跳转传参,例如
// 传入参数 id=2&type=test
Taro.navigateTo({
url: "/pages/page/path/name?id=2&type=test"
});
这样的话,在跳转成功的目标页的生命周期方法里就能通过 this.$router.params
获取到传入的参数,例如上述跳转,在目标页的 componentWillMount
生命周期里获取入参
class C extends Taro.Component {
componentWillMount() {
console.log(this.$router.params); // 输出 { id: 2, type: 'test' }
}
}