• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Defining Page Routes<a name="EN-US_TOPIC_0000001063442795"></a>
2
3-   [Building the Page Layout](#section135242911291)
4-   [Setting Page Styles](#section174441114183216)
5-   [Setting the Switch](#section1276711211359)
6
7Many applications consist of more than one page. For example, in a music application, a user taps a song on a music list page, and then needs to jump to the playback page of the song. You need to link these pages through page routing to implement redirection as required.
8
9The page router finds the target page based on the page URI. The following describes how to switch between two pages:
10
111.  In the  **Project**  window of DevEco Studio, choose  **entry \> src \> main**  \>  **js**  \>  **default**. Right-click the  **pages**  folder and choose  **New JS Page**  from the shortcut menu to create the  **detail**  page.
122.  Call  **router.push\(\)**  to navigate users to the  **detail**  page.
133.  Call  **router.back\(\)**  to navigate users to the  **index**  page.
14
15## Building the Page Layout<a name="section135242911291"></a>
16
17The  **index**  and  **detail**  pages each contains a  **<text\>**  component that specifies the current page, and a  **<button\>**  component that implements the switching between two pages. Example code in  **.hml**  files is as follows:
18
19```
20<!-- index.hml -->
21<div class="container">
22  <text class="title">This is the index page.</text>
23  <button type="capsule" value="Go to the second page" class="button" onclick="launch"></button>
24</div>
25```
26
27```
28<!-- detail.hml -->
29<div class="container">
30  <text class="title">This is the detail page.</text>
31  <button type="capsule" value="Go back" class="button" onclick="launch"></button>
32</div>
33```
34
35## Setting Page Styles<a name="section174441114183216"></a>
36
37Set styles for the  **index**  and  **detail**  pages. Centers the  **<text\>**  and  **<button\>**  components and spaces the two components with 50 pixels. The CSS code for the two pages is as follows:
38
39```
40/* index.css */
41/* detail.css */
42.container {
43  flex-direction: column;
44  justify-content: center;
45  align-items: center;
46}
47
48.title {
49  font-size: 50px;
50  margin-bottom: 50px;
51}
52```
53
54## Setting the Switch<a name="section1276711211359"></a>
55
56To make the  **launch**  method of the  **<button\>**  component take effect, the redirection logic needs to be implemented in the  **.js**  file of the page. Call  **router.push\(\)**  to add the page URI to the route stack, that is, to jump to the page specified by the URI. You need to import the router module before calling the router method. The sample code is as follows:
57
58```
59// index.js
60import router from '@system.router';
61export default {
62  launch() {
63    router.push ({
64      uri: 'pages/detail/detail',
65    });
66  },
67}
68```
69
70```
71// detail.js
72import router from '@system.router';
73export default {
74  launch() {
75    router.back();
76  },
77}
78```
79
80The following figure shows the effect.
81
82**Figure  1**  Page routing<a name="fig41915914355"></a>
83![](figures/page-routing.png "page-routing")
84
85