HarmonyOS 获取网页的Title 应该怎么获取

获取网页的Title 应该怎么获取这是我的自定义标题栏 Text(this.title) 中的router 传值带过来的

Row() { 
  Image($r('app.media.ic_title_back_arrow')) 
    .width('5%') 
    .height(20) 
    .onClick(() => { 
      router.back() 
    }) 
  Image($r('app.media.actionbar_finish')) 
    .width('5%') 
    .height(30) 
    .margin({left :'2.5%' }) 
    .onClick(() => { 
      router.back() 
    }) 
  Text(this.title) 
    .fontSize('20fp') 
    .width('80%') 
    .maxLines(1) 
    .textOverflow({overflow: TextOverflow.Ellipsis}) 
    .textAlign(TextAlign.Center) 
    .margin({right :'5%' }) 
  Image($r('app.media.more_menu')) 
    .width('5%') 
    .height(20) 
    .margin({right :'2.5%' }) 
    .onClick(() => { 
      router.back() 
    }) 
} 
.backgroundColor($r('app.color.mainPage_selected')) 
.padding(12) 
.width('100%') 
.height('45fp') 
 
Divider() 
  .strokeWidth('1px') 
  .color($r('sys.color.ohos_id_color_list_separator')) 
}
Web({ src: $rawfile(this.url), controller: this.controller }) 
  .javaScriptAccess(true) 
  .fileAccess(true) 
  .userAgent(this.ua) 
  .imageAccess(true)//设置是否允许自动加载图片资源。 
  .javaScriptProxy({ 
    object: this.jsBridge, 
    name: "jsBridge_jzt", 
    methodList: jsMethodList.day, 
    controller: this.controller 
  })
  • 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.
HarmonyOS
2024-08-08 12:16:17
913浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

下面是一个获取页面title的简单demo

import router from '@ohos.router'; 
import web_webview from '@ohos.web.webview'; 
 
@Entry 
@Component 
struct TitlePage { 
  @State title: string = (router.getParams() as Record<string, number>)['type'] == 1 ? (router.getParams() as Record<string, string>)['title'] : ''; 
 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
 
  aboutToAppear(): void { 
    console.log("title: " + this.title); 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.title) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
 
        Web({ src: $rawfile('index.html'), controller: this.controller }) 
          .onPageEnd(() => { 
            this.title = this.controller.getTitle(); 
            console.log("title: " + this.title); 
          }) 
 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-08-08 20:01:30
相关问题
HarmonyOS web组件获取title为当前链接
1063浏览 • 1回复 待解决
HarmonyOS NavDestinationtitle怎么居中
826浏览 • 1回复 待解决
HarmonyOS Navigation中title怎么居中显示
847浏览 • 1回复 待解决
wifinetworkid怎么获取
3907浏览 • 1回复 待解决
HarmonyOS Sendable装饰对象怎么获取
683浏览 • 1回复 待解决
HarmonyOS怎么获取系统相册路径
1035浏览 • 1回复 待解决
HarmonyOS 怎么获取当前系统语言
1138浏览 • 1回复 待解决