弹窗 AlertDialog AlertDialog是系统原生弹窗,一般用来给用户展示提示信息,或者让用户做确认操作,在操作完成(比如登录成功/失败、注册完成)后非常常用。代码示例:// 在按钮点击 📅 2026/7/1 2:29:37 弹窗AlertDialogAlertDialog是系统原生弹窗一般用来给用户展示提示信息或者让用户做确认操作在操作完成比如登录成功/失败、注册完成后非常常用。代码示例// 在按钮点击事件中弹出弹窗Button(登录).onClick(() {if (this.username admin this.password 123456) {// 登录成功弹窗AlertDialog.show({title: 登录成功,message: 欢迎回来admin,confirm: {value: 确定,action: () {console.log(用户点击确认);}}})} else {// 登录失败弹窗AlertDialog.show({title: 登录失败,message: 账号或密码错误请重新输入,confirm: {value: 确定,action: () {}}})}})登录结果弹窗运行效果图3. 路由router路由用来实现多页面跳转和参数传递一个应用一般会包含多个页面通过路由管理页面的跳转、回退和数据传递。首先要导入路由模块import router from ohos.router;跳转router.pushUrl({ url: pages/页面名, params: { 参数key: 参数值 } })接收参数在目标页面的onPageShow生命周期中通过router.getParams()获取参数所有需要跳转的页面必须在项目根目录的main_pages.json中注册否则无法跳转代码示例// 1. 注册页点击提交后跳转登录页并传递注册信息import router fromohos.router;EntryComponentstruct RegisterPage {Stateusername: string ;Statepassword: string ;build() {// 省略表单代码...Button(注册并登录).onClick(() {router.pushUrl({url: pages/LoginPage,params: {account: this.username,pwd: this.password}})})}}代码示例// 2. 登录页接收注册页传递的参数import router fromohos.router;EntryComponentstruct LoginPage {Stateaccount: string ;onPageShow() {// 页面显示时获取传递过来的参数const params router.getParams() asany;if (params.account) {this.account params.account;}}build() {// 省略登录表单代码...}}注意 必须在这里把所有页面路径添加到src数组中路由才能正常跳转例如json文件示例{src:[pages/Index,pages/RegisterPage,pages/LoginPage]}页面跳转流程图