通过routerName跳转
思路:
当点击 id
为 ThirdHelloWorld
的 Row
组件时,会尝试通过 router.pushNamedRoute
方法跳转到名为 PersonalInformation
的页面。如果跳转成功,不会有额外操作,如果跳转过程中出现错误(捕获到 BusinessError
类型的错误),会将错误信息打印到控制台。
想要进行跳转的page
1.先进行import '@ohos/xxxx' xxx为路径
2.再去pushNamedRoute目标的name
import router from '@ohos.router';
import { BusinessError } from '@kit.BasicServicesKit';
import '@ohos/negative/src/main/ets/pages/PersonalInformation';
import '@ohos/negative/src/main/ets/pages/DeviceManagement';
import '@ohos/negative/src/main/ets/pages/DeviceDetails';
@Component
export struct NegativeOneScreen {
build() {
RelativeContainer() {
// 头部
Row() {
Column({ space: 5 }) {
Text()
.margin({
left: 10
})
Text().fontSize(20).fontColor(Color.White)
.margin({
top: 5,
left: 20
})
}
.margin({ top: 10, bottom: 5 })
.alignRules({
left: { anchor: 'image', align: HorizontalAlign.Start }
})
}
.onClick(() => {
router.pushNamedRoute({ name: 'PersonalInformation' }).then(() => {
}).catch((error: BusinessError) => {
console.info(error.message);
})
})
.id('ThirdHelloWorld').height(100).width('80%').margin({ left: 20 })
}
.height('100%').width('100%').backgroundColor('#ff1477b3')
}
}
目标Page
1.添加@Entry({routeName:'xxx'}) xxx为目标page的name
// 导入页面路由模块
import router from '@ohos.router';
import { BusinessError } from '@kit.BasicServicesKit';
import { InputMethodListDialog } from '@ohos.inputMethodList';
@Component
@Entry({routeName:'PersonalInformation'})
export struct PersonalInformation {
build() {
RelativeContainer() {
}
.width('100%')
.height('100%')
}
}