1# Implementing Page Redirection and Data Transmission 2 3This section describes how to implement page redirection and data transmission between pages: 4 5 61. 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. 7 82. Data transmission between pages: After you click a food item, **FoodDetail** receives data from the previous page and renders the corresponding food details page. 9 10 11## Page Redirection 12 13The declarative UI paradigm provides two mechanisms for page redirection: 14 151. **Navigator**: encapsulates the page routing capability. After the page target is specified, all child components in the page target have the routing capability. 16 172. Router APIs: called to implement various operations of page routing. You'll need to import **router** before calling the router APIs. 18 19The procedure below uses these two mechanisms for redirection between the page list page and food details page. 20 211. 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 23 @Component 24 struct FoodListItem { 25 private foodItem: FoodData 26 build() { 27 Navigator({ target: 'pages/FoodDetail' }) { 28 Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { 29 Image(this.foodItem.image) 30 .objectFit(ImageFit.Contain) 31 .height(40) 32 .width(40) 33 .backgroundColor('#FFf1f3f5') 34 .margin({ right: 16 }) 35 Text(this.foodItem.name) 36 .fontSize(14) 37 .flexGrow(1) 38 Text(this.foodItem.calories + ' kcal') 39 .fontSize(14) 40 } 41 .height(64) 42 } 43 .margin({ right: 24, left:32 }) 44 } 45 } 46 ``` 47 48  49 502. 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 ```ts 52 import router from '@ohos.router' 53 54 @Component 55 struct FoodGridItem { 56 private foodItem: FoodData 57 build() { 58 Column() { 59 ...... 60 } 61 .height(184) 62 .width('100%') 63 .onClick(() => { 64 router.pushUrl({ url: 'pages/FoodDetail' }) 65 }) 66 } 67 } 68 ``` 69 70  71 723. 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 74 // FoodDetail.ets 75 import router from '@ohos.router' 76 77 @Component 78 struct PageTitle { 79 build() { 80 Flex({ alignItems: ItemAlign.Start }) { 81 Image($r('app.media.Back')) 82 .width(21.8) 83 .height(19.6) 84 Text('Food Detail') 85 .fontSize(21.8) 86 .margin({left: 17.4}) 87 } 88 .height(61) 89 .backgroundColor('#FFedf2f5') 90 .padding({ top: 13, bottom: 15, left: 28.3 }) 91 .onClick(() => { 92 router.back() 93 }) 94 } 95 } 96 ``` 97 984. Create the Stack component in the **FoodDetail** component, including the **FoodImageDisplay** and **PageTitle** child components. Set the alignment mode to **TopStart**. 99 ```ts 100 @Entry 101 @Component 102 struct FoodDetail { 103 build() { 104 Column() { 105 Stack( { alignContent: Alignment.TopStart }) { 106 FoodImageDisplay() 107 PageTitle() 108 } 109 ContentTable() 110 } 111 .alignItems(HorizontalAlign.Center) 112 } 113 } 114 ``` 115 116  117 118 119## Data Transmission Between Pages 120 121We 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: 122 1231. Set the **params** attribute, which accepts the key-value object, in the **Navigator** of the **FoodListItem** component. 124 ```ts 125 // FoodList.ets 126 @Component 127 struct FoodListItem { 128 private foodItem: FoodData 129 build() { 130 Navigator({ target: 'pages/FoodDetail' }) { 131 ...... 132 } 133 .params({ foodData: this.foodItem }) 134 } 135 } 136 ``` 137 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**. 139 140 ```ts 141 router.pushUrl({ 142 url: 'pages/FoodDetail', 143 params: { foodData: this.foodItem } 144 }) 145 ``` 146 1472. Import the **FoodData** class to the **FoodDetail** page and add the **foodItem** member variable to the **FoodDetail** component. 148 ```ts 149 // FoodDetail.ets 150 import { FoodData } from '../model/FoodData' 151 152 @Entry 153 @Component 154 struct FoodDetail { 155 private foodItem: FoodData 156 build() { 157 ...... 158 } 159 } 160 ``` 161 1623. 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 164 @Entry 165 @Component 166 struct FoodDetail { 167 private foodItem: FoodData = router.getParams()['foodData'] 168 169 build() { 170 ...... 171 } 172 } 173 ``` 174 1754. 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 177 @Component 178 struct PageTitle { 179 build() { 180 Flex({ alignItems: ItemAlign.Start }) { 181 Image($r('app.media.Back')) 182 .width(21.8) 183 .height(19.6) 184 Text('Food Detail') 185 .fontSize(21.8) 186 .margin({left: 17.4}) 187 } 188 .height(61) 189 .backgroundColor('#FFedf2f5') 190 .padding({ top: 13, bottom: 15, left: 28.3 }) 191 .onClick(() => { 192 router.back() 193 }) 194 } 195 } 196 197 @Component 198 struct FoodImageDisplay { 199 private foodItem: FoodData 200 build() { 201 Stack({ alignContent: Alignment.BottomStart }) { 202 Image(this.foodItem.image) 203 .objectFit(ImageFit.Contain) 204 Text(this.foodItem.name) 205 .fontSize(26) 206 .fontWeight(500) 207 .margin({ left: 26, bottom: 17.4 }) 208 } 209 .height(357) 210 .backgroundColor('#FFedf2f5') 211 } 212 } 213 214 @Component 215 struct ContentTable { 216 private foodItem: FoodData 217 218 @Builder IngredientItem(title:string, name: string, value: string) { 219 Flex() { 220 Text(title) 221 .fontSize(17.4) 222 .fontWeight(FontWeight.Bold) 223 .layoutWeight(1) 224 Flex() { 225 Text(name) 226 .fontSize(17.4) 227 .flexGrow(1) 228 Text(value) 229 .fontSize(17.4) 230 } 231 .layoutWeight(2) 232 } 233 } 234 235 build() { 236 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 237 this.IngredientItem('Calories', 'Calories', this.foodItem.calories + 'kcal') 238 this.IngredientItem('Nutrition', 'Protein', this.foodItem.protein + 'g') 239 this.IngredientItem('', 'Fat', this.foodItem.fat + 'g') 240 this.IngredientItem('', 'Carbohydrates', this.foodItem.carbohydrates + 'g') 241 this.IngredientItem('', 'VitaminC', this.foodItem.vitaminC + 'mg') 242 } 243 .height(280) 244 .padding({ top: 30, right: 30, left: 30 }) 245 } 246 } 247 248 @Entry 249 @Component 250 struct FoodDetail { 251 private foodItem: FoodData = router.getParams()['foodData'] 252 253 build() { 254 Column() { 255 Stack( { alignContent: Alignment.TopStart }) { 256 FoodImageDisplay({ foodItem: this.foodItem }) 257 PageTitle() 258 } 259 ContentTable({ foodItem: this.foodItem }) 260 } 261 .alignItems(HorizontalAlign.Center) 262 } 263 } 264 ``` 265