在HarmonyOS开发中 router.back无法退出应用 ?

阅读 531
1 个回答

具体解决方案:

import { common } from '@kit.AbilityKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
@Entry 
@Component 
struct ExitPage { 
  @State message: string = 'Hello World'; 
  context = getContext(this) as common.UIAbilityContext; 
 
  Exit() { 
    try { 
      this.context.terminateSelf((err: BusinessError) => { 
        if (err.code) { 
          // 处理业务逻辑错误 
          console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`); 
          return; 
        } 
        // 执行正常业务 
        console.info('terminateSelf succeed'); 
      }); 
    } catch (err) { 
      // 捕获同步的参数错误 
      let code = (err as BusinessError).code; 
      let message = (err as BusinessError).message; 
      console.error(`terminateSelf failed, code is ${code}, message is ${message}`); 
    } 
  } 
 
  build() { 
    Column() { 
      Text(this.message) 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
      Button('退出应用') 
        .onClick(() => { 
          this.Exit() 
        }) 
    } 
    .width('100%') 
  } 
}

具体可参考一下链接:
https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进