Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
7ef0869c
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
7ef0869c
编写于
4月 17, 2023
作者:
O
openharmony_ci
提交者:
Gitee
4月 17, 2023
浏览文件
操作
浏览文件
下载
差异文件
!17324 modify serializeWebState&RestoreWebState sample code(3.2release)
Merge pull request !17324 from 李想/cherry-pick-1681467563
上级
02e20571
c1d1b173
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
52 addition
and
13 deletion
+52
-13
zh-cn/application-dev/reference/apis/js-apis-webview.md
zh-cn/application-dev/reference/apis/js-apis-webview.md
+52
-13
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-webview.md
浏览文件 @
7ef0869c
...
...
@@ -3056,10 +3056,11 @@ serializeWebState(): Uint8Array
**示例:**
1.
对文件的操作需要导入文件管理模块,详情请参考
[
文件管理
](
./js-apis-file-fs.md
)
。
```
ts
// xxx.ets
import
web_webview
from
'
@ohos.web.webview
'
;
import
f
ileio
from
'
@ohos.fileio
'
;
import
f
s
from
'
@ohos.file.fs
'
;
@
Entry
@
Component
...
...
@@ -3072,11 +3073,13 @@ struct WebComponent {
.
onClick
(()
=>
{
try
{
let
state
=
this
.
controller
.
serializeWebState
();
let
path
=
globalThis
.
AbilityContext
.
cacheDir
;
// globalThis.cacheDir从MainAbility.ts中获取。
let
path
=
globalThis
.
cacheDir
;
path
+=
'
/WebState
'
;
let
fd
=
fileio
.
openSync
(
path
,
0o2
|
0o100
,
0o666
);
fileio
.
writeSync
(
fd
,
state
.
buffer
);
fileio
.
closeSync
(
fd
);
// 以同步方法打开文件。
let
file
=
fs
.
openSync
(
path
,
fs
.
OpenMode
.
READ_WRITE
|
fs
.
OpenMode
.
CREATE
);
fs
.
writeSync
(
file
.
fd
,
state
.
buffer
);
fs
.
closeSync
(
file
.
fd
);
}
catch
(
error
)
{
console
.
error
(
`ErrorCode:
${
error
.
code
}
, Message:
${
error
.
message
}
`
);
}
...
...
@@ -3087,6 +3090,21 @@ struct WebComponent {
}
```
2.
修改MainAbility.ts。
获取应用缓存文件路径。
```
ts
// xxx.ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
web_webview
from
'
@ohos.web.webview
'
;
export
default
class
MainAbility
extends
UIAbility
{
onCreate
(
want
,
launchParam
)
{
// 通过在globalThis对象上绑定cacheDir,可以实现UIAbility组件与Page之间的数据同步。
globalThis
.
cacheDir
=
this
.
context
.
cacheDir
;
}
}
```
### restoreWebState
restoreWebState(state: Uint8Array): void
...
...
@@ -3111,10 +3129,11 @@ restoreWebState(state: Uint8Array): void
**示例:**
1.
对文件的操作需要导入文件管理模块,详情请参考
[
文件管理
](
./js-apis-file-fs.md
)
。
```
ts
// xxx.ets
import
web_webview
from
'
@ohos.web.webview
'
;
import
f
ileio
from
'
@ohos.fileio
'
;
import
f
s
from
'
@ohos.file.fs
'
;
@
Entry
@
Component
...
...
@@ -3126,17 +3145,22 @@ struct WebComponent {
Button
(
'
RestoreWebState
'
)
.
onClick
(()
=>
{
try
{
let
path
=
globalThis
.
AbilityContext
.
cacheDir
;
// globalThis.cacheDir从MainAbility.ts中获取。
let
path
=
globalThis
.
cacheDir
;
path
+=
'
/WebState
'
;
let
fd
=
fileio
.
openSync
(
path
,
0o002
,
0o666
);
let
stat
=
fileio
.
fstatSync
(
fd
);
// 以同步方法打开文件。
let
file
=
fs
.
openSync
(
path
,
fs
.
OpenMode
.
READ_WRITE
);
let
stat
=
fs
.
statSync
(
path
);
let
size
=
stat
.
size
;
let
buf
=
new
ArrayBuffer
(
size
);
fileio
.
read
(
fd
,
buf
,
(
err
,
data
)
=>
{
if
(
data
)
{
this
.
controller
.
restoreWebState
(
new
Uint8Array
(
data
.
buffer
));
fs
.
read
(
file
.
fd
,
buf
,
(
err
,
readLen
)
=>
{
if
(
err
)
{
console
.
info
(
"
mkdir failed with error message:
"
+
err
.
message
+
"
, error code:
"
+
err
.
code
);
}
else
{
console
.
info
(
"
read file data succeed
"
);
this
.
controller
.
restoreWebState
(
new
Uint8Array
(
buf
.
slice
(
0
,
readLen
)));
fs
.
closeSync
(
file
);
}
fileio
.
closeSync
(
fd
);
});
}
catch
(
error
)
{
console
.
error
(
`ErrorCode:
${
error
.
code
}
, Message:
${
error
.
message
}
`
);
...
...
@@ -3148,6 +3172,21 @@ struct WebComponent {
}
```
2.
修改MainAbility.ts。
获取应用缓存文件路径。
```
ts
// xxx.ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
web_webview
from
'
@ohos.web.webview
'
;
export
default
class
MainAbility
extends
UIAbility
{
onCreate
(
want
,
launchParam
)
{
// 通过在globalThis对象上绑定cacheDir,可以实现UIAbility组件与Page之间的数据同步。
globalThis
.
cacheDir
=
this
.
context
.
cacheDir
;
}
}
```
### customizeSchemes
static customizeSchemes(schemes: Array
\<
WebCustomScheme
\>
): void
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录