电子说
import common from '@ohos.app.ability.common';
import router from '@ohos.router'
DevEco Studio 3.1.1 Release
api 9
import MyRouterUtil from '../common/utils/MyRouterUtil'
MyRouterUtil.openBrowser('https://www.baidu.com')
export default class MyRouterUtil {
//日志过滤器
static readonly LOG: string = "====MyRouterUtil"
/**
* 鸿蒙APP跳转到手机浏览器并打开网页
* @param url 打开网页后展示的url地址
*/
public static async openBrowser(url: string) {
try {
let want = {
"action": "ohos.want.action.viewData",
"entities": ["entity.system.browsable"],
"uri": url,
"type": "text/plain"
}
let context = getContext(this) as common.UIAbilityContext;
await context.startAbility(want)
console.info(MyRouterUtil.LOG, 'openBrowser', url)
} catch (error) {
console.error(MyRouterUtil.LOG, 'openBrowser', `${error.code},url:${url}`)
}
}
public static backPage() {
router.back()
console.info(MyRouterUtil.LOG, 'backPage', '判断栈内页面数:', router.getLength())
}
在鸿蒙APP内打开网页
常用来打开隐私声明、用户协议等
@param url
public static async openWeb(url: string) {
try {
await router.pushUrl({
url: 'pages/Web', //这个页面是我自己创建的,用于展示网页。里面可根据url的参数决定是否显示原生的标题栏。
params: {
launchWebview: url
}
}, router.RouterMode.Standard)
console.info(MyRouterUtil.LOG, 'openWeb', url)
} catch (e) {
console.error(MyRouterUtil.LOG, 'openWeb', JSON.stringify(e), 'url:', url)
}
}
鸿蒙OS开发 | 更多内容↓点击 | HarmonyOS与OpenHarmony技术 |
---|---|---|
鸿蒙技术文档 | 开发知识更新库gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md 在这。 | 或+mau123789学习,是v喔 |
public static getParamsByWeb() {
let url = JSON.parse(JSON.stringify(router.getParams())).launchWebview
console.info(MyRouterUtil.LOG, 'getParamsByWeb', url)
return url
}
public static async openIndex() {
try {
await router.pushUrl({
url: 'pages/Index'
}, router.RouterMode.Single)
console.info(MyRouterUtil.LOG, 'openIndex', 'succ')
router.clear()
} catch (e) {
console.error(MyRouterUtil.LOG, 'openIndex', JSON.stringify(e))
}
}
public static async openLogin() {
try {
await router.pushUrl({
url: 'pages/Login'
}, router.RouterMode.Standard)
console.info(MyRouterUtil.LOG, 'openLogin', 'succ')
} catch (e) {
console.error(MyRouterUtil.LOG, 'openLogin', JSON.stringify(e))
}
}
}
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !