在HarmonyOS NEXT中 当app进程不结束的情况下,再次进入这些page的时候,就不执行这些方法了的解决方法?

阅读 629
1 个回答

解决方案,参考demo:

//Index.ets 
import web_webview from '@ohos.web.webview' 
import { router } from '@kit.ArkUI'; 
 
import { Count } from './Count' 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
 
  aboutToAppear(): void { 
    // 执行一次 
 
    if (Count.count === 0) { 
      console.info('1111111111111') 
      Count.count = 1 
    } 
 
  } 
 
  build() { 
    Column() { 
      Text(this.message) 
        .id('HelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: '__container__', align: VerticalAlign.Center }, 
          middle: { anchor: '__container__', align: HorizontalAlign.Center } 
        }) 
        .onClick(() => { 
          router.pushUrl({ 
            url: 'pages/Page' 
          }) 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
} 
 
//Count.ets 
export class  Count{ 
  static   count:number = 0 
} 
//Page.ets 
import { router } from '@kit.ArkUI'; 
 
@Entry 
@Component 
struct Page { 
  @State message: string = 'Hello World'; 
 
  build() { 
    RelativeContainer() { 
      Text(this.message) 
        .id('PageHelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: '__container__', align: VerticalAlign.Center }, 
          middle: { anchor: '__container__', align: HorizontalAlign.Center } 
        }) 
        .onClick(() => { 
          router.pushUrl({ 
            url: 'pages/Index' 
          }) 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}

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

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