将web封装成一个组件用于加载字符串html,html代码中行内式样式无法解析或者解析无效。
@Component
export struct ReserveWeb {
webController: web_view.WebviewController = new webview.WebviewController();
@Prop reserveClause: string = "";
build() {
Column() {
Web({
src: "xxx",
controller: this.webController
})
.javaScriptAccess(true)//设置是否允许执行 JS 脚本,默认为 true ,表示允许执行。
.fileAccess(true)//设置是否开启通过 $rawfile(filepath/filename) 访问应用中 rawfile 路径的文件
.onlineImageAccess(true)//设置是否允许从网络加载图片资源(通过 HTTP 和 HTTPS 访问的资源)
.domStorageAccess(true)//设置是否开启文档对象模型存储接口(DOM Storage API)权限
.imageAccess(true)//设置是否允许自动加载图片资源
.zoomAccess(true)
.cacheMode(CacheMode.None)//加载资源使用cache,如果cache中无该资源则从网络中获取。
.mixedMode(MixedMode.All)//设置是否允许加载超文本传输协议(HTTP)和超文本传输安全协议(HTTPS)混合内容
.overviewModeAccess(true)
.geolocationAccess(false)//设置是否开启获取地理位置权限,默认开启。
.onControllerAttached(() => {
setTimeout(() => {
let data = "<html><body>" + this.reserveClause + "</body></html>"
this.webController.loadData(
data,
"text/html",
"UTF-8"
)
}, 100)
})
}
}
}
"<p style=\"font-family: PingFangSC-Medium, PingFang SC;font-size: 15px;font-weight: 500;color: #222222;margin: 0 0 10px;\">"
" 1、xxxx</p>"
"<p style=\"font-family: PingFangSC-Regular, PingFang SC;margin: 0 0 5px;\"><span style=\"font-size: 13px;color: #666666;\">文本内容;</span>"
"</p>"
"<p style=\"font-family: PingFangSC-Regular, PingFang SC;margin: 0 0 5px;\"><span style=\"font-size: 13px;color: #666666;\">文本内容;</span>"
"</p>"
"<p style=\"font-family: PingFangSC-Regular, PingFang SC;margin: 0 0 5px;\"><span style=\"font-size: 13px;color: #666666;\">文本内容;</span>"
"</p>"
"<p style=\"font-family: PingFangSC-Regular, PingFang SC;margin: 0 0 10px;\"><span style=\"font-size: 13px;color: #666666;\">文本内容。</span>"
"</p>"
"<p style=\"font-family: PingFangSC-Medium, PingFang SC;font-size: 15px;font-weight: 500;color: #222222;margin: 0 0 10px;\">"
" 2、文本内容</p>"
"<p style=\"font-family: PingFangSC-Regular, PingFang SC;margin: 0 0 10px;\"><span style=\"font-size: 13px;color: #666666;\">文本内容</span>"
"</p>"
"<p style=\"font-family: PingFangSC-Medium, PingFang SC;font-size: 15px;font-weight: 500;color: #222222;margin: 0 0 10px;\">"
" 3、文本内容</p>"
"<p style=\"font-family: PingFangSC-Medium, PingFang SC;font-size: 15px;font-weight: 500;color: #222222;\">"
" 4、文本内容</p>"
需要加个meta标签,如以下demo: