W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
路由容器組件,提供路由跳轉(zhuǎn)能力。
該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
Navigator(value?: {target: string, type?: NavigationType})
參數(shù):
參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
---|---|---|---|
target | string | 是 | 指定跳轉(zhuǎn)目標(biāo)頁面的路徑。 |
type | NavigationType | 否 | 指定路由方式。 默認(rèn)值:NavigationType.Push |
名稱 | 描述 |
---|---|
Push | 跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。 |
Replace | 用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。 |
Back | 返回到指定的頁面。指定的頁面不存在棧中時(shí)不響應(yīng)。未傳入指定的頁面時(shí)返回上一頁。 |
名稱 | 參數(shù) | 描述 |
---|---|---|
active | boolean | 當(dāng)前路由組件是否處于激活狀態(tài),處于激活狀態(tài)時(shí),會(huì)生效相應(yīng)的路由操作。 |
params | object | 跳轉(zhuǎn)時(shí)要同時(shí)傳遞到目標(biāo)頁面的數(shù)據(jù),可在目標(biāo)頁面使用router.getParams()獲得。 |
target | string | 設(shè)置跳轉(zhuǎn)目標(biāo)頁面的路徑。 目標(biāo)頁面需加入main_pages.json文件中。 |
type | NavigationType | 設(shè)置路由方式。 默認(rèn)值:NavigationType.Push |
- // Navigator.ets
- @Entry
- @Component
- struct NavigatorExample {
- @State active: boolean = false
- @State Text: object = {name: 'news'}
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
- Navigator({ target: 'pages/container/navigator/Detail', type: NavigationType.Push }) {
- Text('Go to ' + this.Text['name'] + ' page')
- .width('100%').textAlign(TextAlign.Center)
- }.params({ text: this.Text }) // 傳參數(shù)到Detail頁面
- Navigator() {
- Text('Back to previous page').width('100%').textAlign(TextAlign.Center)
- }.active(this.active)
- .onClick(() => {
- this.active = true
- })
- }.height(150).width(350).padding(35)
- }
- }
- // Detail.ets
- import router from '@ohos.router'
- @Entry
- @Component
- struct DetailExample {
- // 接收Navigator.ets的傳參
- @State text: any = router.getParams()['text']
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
- Navigator({ target: 'pages/container/navigator/Back', type: NavigationType.Push }) {
- Text('Go to back page').width('100%').height(20)
- }
- Text('This is ' + this.text['name'] + ' page')
- .width('100%').textAlign(TextAlign.Center)
- }
- .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
- }
- }
- // Back.ets
- @Entry
- @Component
- struct BackExample {
- build() {
- Column() {
- Navigator({ target: 'pages/container/navigator/Navigator', type: NavigationType.Back }) {
- Text('Return to Navigator Page').width('100%').textAlign(TextAlign.Center)
- }
- }.width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: