HarmonyOS 如何实现web内点击超链接,跳转新界面?

希望点击web中的超链接,跳转一个新的界面去展示链接内容,具体怎么实现呢?

阅读 478
1 个回答

通过onLoadIntercept拦截接口可以根据业务做相应处理。

import web_webview from '@ohos.web.webview';
import { common, Want } from '@kit.AbilityKit';

@Entry
@Component
struct WebComponent {
  webviewController: web_webview.WebviewController = new web_webview.WebviewController();

  build() {
    Column() {
      Web({ src: $rawfile('local.html'), controller: this.webviewController})
        .onLoadIntercept((event) => {
          if (event) {
            let url: string = event.data.getRequestUrl();
            if (url.indexOf('store://') === 0) {
              const want: Want = {
                uri: `store://appgallery.huawei.com/`
              }
              const context = getContext(this) as common.UIAbilityContext;
              context.startAbility(want).then(() => {
              }).catch(() => {
              })
              return true;
            }
          }
          return false;
        })
    }
  }
}
<!-- local.html -->
  <!DOCTYPE html>
  <html>
  <body>
  <p>Hello World</p>
  <br><a>P3-</a><a href="store://appgallery.huawei.com/">应用市场</a></div>
  </body>
  </html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进