title: Routing Features

Routing API Description

In Taro, the routing feature comes by default and does not require additional routing configuration by the developer.

We just need to specify pages in the config configuration of the entry file, and then we can jump to the destination page in our code via the API provided by Taro, e.g.

  1. // Jump to the destination page and open a new page
  2. Taro.navigateTo({
  3. url: '/pages/page/path/name'
  4. })
  5. // Jump to the destination page and open on the current page
  6. Taro.redirectTo({
  7. url: '/pages/page/path/name'
  8. })

For specific API descriptions, please see the navigationsection for instructions.

Route Passing Parameters

We can do this by adding a query string parameter to all jumps after the url, for example

  1. // Pass in parameters id=2&type=test
  2. Taro.navigateTo({
  3. url: '/pages/page/path/name?id=2&type=test'
  4. })

In this case, the incoming parameters can be retrieved in the lifecycle method of the target page of the successful jump via getCurrentInstance().router.params, such as the above jump, in the target page’s componentWillMount (or Vue’s created) life cycle of the target page:

  1. import { getCurrentInstance } from '@tarojs/taro'
  2. import React, { Component } from 'react'
  3. export default class C extends Component {
  4. componentDidMount () {
  5. console.log(getCurrentInstance().router.params) // output { id: 2, type: 'test' }
  6. }
  7. }