ui-ts-page-redirection-data-transmission.md 8.5 KB
Newer Older
G
ge-yafang 已提交
1
# Implementing Page Redirection and Data Transmission
Z
zengyawen 已提交
2

G
ge-yafang 已提交
3
This section describes how to implement page redirection and data transmission between pages:
Z
zengyawen 已提交
4 5


G
ge-yafang 已提交
6
1. Page redirection: Click a food item on the food list page to go to the food details page. Click the back button on the food details page to go back to the food list page.
Z
zengyawen 已提交
7

E
ester.zhou 已提交
8
2. Data transmission between pages: After you click a food item, **FoodDetail** receives data from the previous page and renders the corresponding food details page.
Z
zengyawen 已提交
9 10


G
ge-yafang 已提交
11
## Page Redirection
Z
zengyawen 已提交
12

G
ge-yafang 已提交
13
The declarative UI paradigm provides two mechanisms for page redirection:
Z
zengyawen 已提交
14

E
ester.zhou 已提交
15
1. **Navigator**: encapsulates the page routing capability. After the page target is specified, all child components in the page target have the routing capability.
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
2. Router APIs: called to implement various operations of page routing. You'll need to import **router** before calling the router APIs.
Z
zengyawen 已提交
18

G
ge-yafang 已提交
19
The procedure below uses these two mechanisms for redirection between the page list page and food details page.
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21
1. Click **FoodListItem**. The **FoodDetail** page is displayed. Create a **Navigator** component in **FoodListItem** to enable its child components to have the routing function. The target page is **'pages/FoodDetail'**.
22
   ```ts
G
ge-yafang 已提交
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
   @Component
   struct FoodListItem {
     private foodItem: FoodData
     build() {
       Navigator({ target: 'pages/FoodDetail' }) {
         Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
           Image(this.foodItem.image)
             .objectFit(ImageFit.Contain)
             .height(40)
             .width(40)
             .backgroundColor('#FFf1f3f5')
             .margin({ right: 16 })
           Text(this.foodItem.name)
             .fontSize(14)
             .flexGrow(1)
           Text(this.foodItem.calories + ' kcal')
             .fontSize(14)
         }
         .height(64)
       }
       .margin({ right: 24, left:32 })
     }
   }
   ```

   ![en-us_image_0000001223127744](figures/en-us_image_0000001223127744.gif)

E
ester.zhou 已提交
50
2. Click **FoodGridItem**. The **FoodDetail** page is displayed. Import the **router** module, and then call the **push** API of this module to push the **FoodDetail** page to the route stack to implement page redirection.
51 52
   ```ts
   import router from '@ohos.router'
H
HelloCrease 已提交
53

G
ge-yafang 已提交
54 55 56 57 58 59 60 61 62 63
   @Component
   struct FoodGridItem {
     private foodItem: FoodData
     build() {
       Column() {
         ......
       }
       .height(184)
       .width('100%')
       .onClick(() => {
64
         router.pushUrl({ url: 'pages/FoodDetail' })
G
ge-yafang 已提交
65 66 67 68 69 70 71
       })
     }
   }
   ```

   ![en-us_image_0000001267607909](figures/en-us_image_0000001267607909.gif)

E
ester.zhou 已提交
72
3. Add the icon for returning from the **FoodDetail** page to the food list page. Save the **Back.png** file to the **resources** > **base** > **media** directory. Create a custom component **PageTitle**, which contains the back icon and Food Detail text. Call the **router.back()** API of the router to display the top page of the route stack, that is, the upper-level page.
73
   ```ts
G
ge-yafang 已提交
74
   // FoodDetail.ets
75
   import router from '@ohos.router'
H
HelloCrease 已提交
76

G
ge-yafang 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
   @Component
   struct PageTitle {
       build() {
           Flex({ alignItems: ItemAlign.Start }) {
               Image($r('app.media.Back'))
                   .width(21.8)
                   .height(19.6)
               Text('Food Detail')
                   .fontSize(21.8)
                   .margin({left: 17.4})
           }
           .height(61)
           .backgroundColor('#FFedf2f5')
           .padding({ top: 13, bottom: 15, left: 28.3 })
           .onClick(() => {
               router.back()
           })
       }
   }
   ```

E
ester.zhou 已提交
98
4. Create the Stack component in the **FoodDetail** component, including the **FoodImageDisplay** and **PageTitle** child components. Set the alignment mode to **TopStart**.
99
   ```ts
G
ge-yafang 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
   @Entry
   @Component
   struct FoodDetail {
     build() {
       Column() {
         Stack( { alignContent: Alignment.TopStart }) {
           FoodImageDisplay()
           PageTitle()
         }
         ContentTable()
       }
       .alignItems(HorizontalAlign.Center)
     }
   }
   ```

   ![en-us_image_0000001267767881](figures/en-us_image_0000001267767881.png)


## Data Transmission Between Pages

E
ester.zhou 已提交
121
We have implemented the redirection and going back of the **FoodCategoryList** and **FoodDetail** pages. At this point, the tomato details page is displayed no matter which **FoodListItem**/**FoodGridItem** is clicked. This is because the data transmission between pages is not yet configured. To configure data transmission between pages, set the routing with parameters as follows:
H
HelloCrease 已提交
122

E
ester.zhou 已提交
123
1. Set the **params** attribute, which accepts the key-value object, in the **Navigator** of the **FoodListItem** component.
124
   ```ts
G
ge-yafang 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
   // FoodList.ets
   @Component
   struct FoodListItem {
     private foodItem: FoodData
     build() {
       Navigator({ target: 'pages/FoodDetail' }) {
         ......
       }
       .params({ foodData: this.foodItem })
     }
   }
   ```

E
ester.zhou 已提交
138
   The router API called by **FoodGridItem** also has supports redirection with parameters. The method of using the router API is similar to that of using the **Navigator**.
H
HelloCrease 已提交
139

140
   ```ts
141
   router.pushUrl({
142
     url: 'pages/FoodDetail',
G
ge-yafang 已提交
143 144 145 146
     params: { foodData: this.foodItem }
   })
   ```

E
ester.zhou 已提交
147
2. Import the **FoodData** class to the **FoodDetail** page and add the **foodItem** member variable to the **FoodDetail** component.
148
   ```ts
G
ge-yafang 已提交
149 150
   // FoodDetail.ets
   import { FoodData } from '../model/FoodData'
H
HelloCrease 已提交
151

G
ge-yafang 已提交
152 153 154 155 156 157 158 159 160 161
   @Entry
   @Component
   struct FoodDetail {
     private foodItem: FoodData
     build() {
       ......
     }
   }
   ```

E
ester.zhou 已提交
162
3. Obtain the value of **foodData**. Call **router.getParams()['foodData']** to obtain the data corresponding to **foodData** carried when the **FoodCategoryList** page is displayed.
163
   ```ts
G
ge-yafang 已提交
164 165 166
   @Entry
   @Component
   struct FoodDetail {
E
ester.zhou 已提交
167
     private foodItem: FoodData = router.getParams()['foodData']
H
HelloCrease 已提交
168

G
ge-yafang 已提交
169 170 171 172 173 174
     build() {
       ......
     }
   }
   ```

E
ester.zhou 已提交
175
4. Re-build the components on the **FoodDetail** page. During building, the food information on the **FoodDetail** page is all directly declared constants. You need to use the passed **FoodData** data to assign a new value to the constants. The sample code is as follows:
176
   ```ts
G
ge-yafang 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
   @Component
   struct PageTitle {
       build() {
           Flex({ alignItems: ItemAlign.Start }) {
               Image($r('app.media.Back'))
                   .width(21.8)
                   .height(19.6)
               Text('Food Detail')
                   .fontSize(21.8)
                   .margin({left: 17.4})
           }
           .height(61)
           .backgroundColor('#FFedf2f5')
           .padding({ top: 13, bottom: 15, left: 28.3 })
           .onClick(() => {
               router.back()
           })
       }
   }
H
HelloCrease 已提交
196

G
ge-yafang 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
   @Component
   struct FoodImageDisplay {
     private foodItem: FoodData
     build() {
       Stack({ alignContent: Alignment.BottomStart }) {
         Image(this.foodItem.image)
           .objectFit(ImageFit.Contain)
         Text(this.foodItem.name)
           .fontSize(26)
           .fontWeight(500)
           .margin({ left: 26, bottom: 17.4 })
       }
       .height(357)
       .backgroundColor('#FFedf2f5')
     }
   }
H
HelloCrease 已提交
213

G
ge-yafang 已提交
214 215 216
   @Component
   struct ContentTable {
     private foodItem: FoodData
H
HelloCrease 已提交
217

G
ge-yafang 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
     @Builder IngredientItem(title:string, name: string, value: string) {
       Flex() {
         Text(title)
           .fontSize(17.4)
           .fontWeight(FontWeight.Bold)
           .layoutWeight(1)
         Flex() {
           Text(name)
             .fontSize(17.4)
             .flexGrow(1)
           Text(value)
             .fontSize(17.4)
         }
         .layoutWeight(2)
       }
     }
H
HelloCrease 已提交
234

G
ge-yafang 已提交
235 236 237 238 239 240 241 242 243 244 245 246
     build() {
       Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) {
         this.IngredientItem('Calories', 'Calories', this.foodItem.calories + 'kcal')
         this.IngredientItem('Nutrition', 'Protein', this.foodItem.protein + 'g')
         this.IngredientItem('', 'Fat', this.foodItem.fat + 'g')
         this.IngredientItem('', 'Carbohydrates', this.foodItem.carbohydrates + 'g')
         this.IngredientItem('', 'VitaminC', this.foodItem.vitaminC + 'mg')
       }
       .height(280)
       .padding({ top: 30, right: 30, left: 30 })
     }
   }
H
HelloCrease 已提交
247

G
ge-yafang 已提交
248 249 250
   @Entry
   @Component
   struct FoodDetail {
E
ester.zhou 已提交
251
     private foodItem: FoodData = router.getParams()['foodData']
H
HelloCrease 已提交
252

G
ge-yafang 已提交
253 254 255 256 257 258 259 260 261 262 263 264
     build() {
       Column() {
         Stack( { alignContent: Alignment.TopStart }) {
           FoodImageDisplay({ foodItem: this.foodItem })
           PageTitle()
         }
         ContentTable({ foodItem: this.foodItem })
       }
       .alignItems(HorizontalAlign.Center)
     }
   }
   ```