目录

How do I enable the Web component to return to the previous web page following a swipe gesture?

Override the onBackPress function to define the return logic and use the accessBackward or accessStep(-1) API provided by WebViewController to determine whether the return operation is allowed on the web page. The code snippet is as follows:

import { webview } from "@kit.ArkWeb";

@Entry
@Component
struct PageOne {
  pageInfos: NavPathStack = new NavPathStack();
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    NavDestination() {
      Column() {
        Web({ src: 'https://www.XXX.com/', controller: this.controller }) // It needs to be manually replaced with the actual website
      }
      .width('100%')
      .height('100%')
    }
    .title('pageOne')
    .onBackPressed(() => {
      if (this.controller.accessBackward()) { // Determine whether the web page can be navigated back
        this.controller.backward() // Navigate back to the previous webpage
        return true
      } else {
        const popDestinationInfo = this.pageInfos.pop(); // Pop the top element of the routing stack 
        return true;
      }
    }).onReady((context: NavDestinationContext) => {
      this.pageInfos = context.pathStack;
    })
  }
}

Reference

accessBackward

accessStep