HarmonyOS Dialog中Web,点击Web中的链接如何新开页面

Dialog中加载Web,点击Web中的链接如何新开页面,而不是在Web中跳转

HarmonyOS
2025-01-09 17:16:51
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

【index.ets】文件

import { router } from '@kit.ArkUI';
import { webview } from '@kit.ArkWeb';

class routerParams {
  src: string
  constructor(str:string) {
    this.src = str;
  }
}

@Entry
@Component
struct DialogFirst {
  controller: webview.WebviewController = new webview.WebviewController();
  @State textValue: string = 'xxxx'
  // 显隐控制设置为不占用
  @State visible: Visibility = Visibility.None
  onPageShow() {
    const params = router.getParams() as Record<string, string>; // 获取传递过来的参数对象
    if (params) {
      this.textValue = params.info as string; // 获取info属性的值
    }
  }
  build() {
    Stack() {
      // 初始页面
      Row() {
        Column() {
          Text('Hello World')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
          Button('click')
            .onClick(() => {
              console.log("hit me!")
              if (this.visible == Visibility.Visible) {
                this.visible = Visibility.None
              } else {
                this.visible = Visibility.Visible
              }
            })
            .backgroundColor(0x777474)
            .fontColor(0x000000)
        }
        .width('100%')
      }
      .height('100%')
      .backgroundColor(0x885555)
      //加了一个半透明灰色的蒙层效果
      Text('')
        .onClick(() => {
          if (this.visible == Visibility.Visible) {
            this.visible = Visibility.None
          } else {
            this.visible = Visibility.Visible
          }
        })
        .width('100%')
        .height('100%')
        .opacity(0.16)
        .backgroundColor(0x000000)
        .visibility(this.visible)
      // 弹框内容
      Column() {
        Column() {
          Web({ src: "http://www.huawei.com", controller: this.controller })
            .javaScriptAccess(true)
            .onOverrideUrlLoading((webResourceRequest: WebResourceRequest) => {
              if (webResourceRequest && webResourceRequest.getRequestUrl() != "https://www.huawei.com/"){
                let url = webResourceRequest.getRequestUrl()
                console.info('===============url=',url)
                router.pushUrl({
                  url: 'pages/Index2',
                  params: new routerParams(url)
                })
                return true;
              }
              return false;
            })
            .height('60%')

        }
        .backgroundColor(0xffffff)
        .visibility(this.visible)
        .clip(true)
        .borderRadius(20)
      }.width('95%') //设置弹窗宽度
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.

【Index2.ets】文件

import { webview } from '@kit.ArkWeb';
import { router } from '@kit.ArkUI';

class routerParams {
  src:string = ''
}

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  dialogController: CustomDialogController | null = null;

  @State params: routerParams = router.getParams() as routerParams;
  @State src: string = this.params.src;

  build() {
    Column() {
      Web({ src: this.src, controller: this.controller })
        .javaScriptAccess(true)
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
分享
微博
QQ
微信
回复
2025-01-09 19:33:22
相关问题
HarmonyOS web页面点击穿透问题
689浏览 • 1回复 待解决
HarmonyOS web加载链接显示效果异常
644浏览 • 1回复 待解决
HarmonyOS web组件 加载web页面异常
1288浏览 • 1回复 待解决
HarmonyOSweb与内嵌vue页面的交互问题
1545浏览 • 1回复 待解决
Web组件预加载,如何实现?
2498浏览 • 1回复 待解决
HarmonyOS web组件获取title为当前链接
1092浏览 • 1回复 待解决
web页面,有什么方式可以拉起APP
997浏览 • 1回复 待解决