Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
7fbf865d
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看板
提交
7fbf865d
编写于
4月 13, 2023
作者:
L
lixiang
1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify serializeWebState&RestoreWebState sample code
Signed-off-by:
N
lixiang
<
lixiang380@huawei.com
>
上级
2ecf880b
变更
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
浏览文件 @
7fbf865d
...
@@ -3588,10 +3588,11 @@ serializeWebState(): Uint8Array
...
@@ -3588,10 +3588,11 @@ serializeWebState(): Uint8Array
**示例:**
**示例:**
1.
对文件的操作需要导入文件管理模块,详情请参考
[
文件管理
](
./js-apis-file-fs.md
)
。
```
ts
```
ts
// xxx.ets
// xxx.ets
import
web_webview
from
'
@ohos.web.webview
'
;
import
web_webview
from
'
@ohos.web.webview
'
;
import
f
ileio
from
'
@ohos.fileio
'
;
import
f
s
from
'
@ohos.file.fs
'
;
@
Entry
@
Entry
@
Component
@
Component
...
@@ -3604,11 +3605,13 @@ struct WebComponent {
...
@@ -3604,11 +3605,13 @@ struct WebComponent {
.
onClick
(()
=>
{
.
onClick
(()
=>
{
try
{
try
{
let
state
=
this
.
controller
.
serializeWebState
();
let
state
=
this
.
controller
.
serializeWebState
();
let
path
=
globalThis
.
AbilityContext
.
cacheDir
;
// globalThis.cacheDir从MainAbility.ts中获取。
let
path
=
globalThis
.
cacheDir
;
path
+=
'
/WebState
'
;
path
+=
'
/WebState
'
;
let
fd
=
fileio
.
openSync
(
path
,
0o2
|
0o100
,
0o666
);
// 以同步方法打开文件。
fileio
.
writeSync
(
fd
,
state
.
buffer
);
let
file
=
fs
.
openSync
(
path
,
fs
.
OpenMode
.
READ_WRITE
|
fs
.
OpenMode
.
CREATE
);
fileio
.
closeSync
(
fd
);
fs
.
writeSync
(
file
.
fd
,
state
.
buffer
);
fs
.
closeSync
(
file
.
fd
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
`ErrorCode:
${
error
.
code
}
, Message:
${
error
.
message
}
`
);
console
.
error
(
`ErrorCode:
${
error
.
code
}
, Message:
${
error
.
message
}
`
);
}
}
...
@@ -3619,6 +3622,21 @@ struct WebComponent {
...
@@ -3619,6 +3622,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
restoreWebState(state: Uint8Array): void
restoreWebState(state: Uint8Array): void
...
@@ -3643,10 +3661,11 @@ restoreWebState(state: Uint8Array): void
...
@@ -3643,10 +3661,11 @@ restoreWebState(state: Uint8Array): void
**示例:**
**示例:**
1.
对文件的操作需要导入文件管理模块,详情请参考
[
文件管理
](
./js-apis-file-fs.md
)
。
```
ts
```
ts
// xxx.ets
// xxx.ets
import
web_webview
from
'
@ohos.web.webview
'
;
import
web_webview
from
'
@ohos.web.webview
'
;
import
f
ileio
from
'
@ohos.fileio
'
;
import
f
s
from
'
@ohos.file.fs
'
;
@
Entry
@
Entry
@
Component
@
Component
...
@@ -3658,17 +3677,22 @@ struct WebComponent {
...
@@ -3658,17 +3677,22 @@ struct WebComponent {
Button
(
'
RestoreWebState
'
)
Button
(
'
RestoreWebState
'
)
.
onClick
(()
=>
{
.
onClick
(()
=>
{
try
{
try
{
let
path
=
globalThis
.
AbilityContext
.
cacheDir
;
// globalThis.cacheDir从MainAbility.ts中获取。
let
path
=
globalThis
.
cacheDir
;
path
+=
'
/WebState
'
;
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
size
=
stat
.
size
;
let
buf
=
new
ArrayBuffer
(
size
);
let
buf
=
new
ArrayBuffer
(
size
);
fileio
.
read
(
fd
,
buf
,
(
err
,
data
)
=>
{
fs
.
read
(
file
.
fd
,
buf
,
(
err
,
readLen
)
=>
{
if
(
data
)
{
if
(
err
)
{
this
.
controller
.
restoreWebState
(
new
Uint8Array
(
data
.
buffer
));
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
)
{
}
catch
(
error
)
{
console
.
error
(
`ErrorCode:
${
error
.
code
}
, Message:
${
error
.
message
}
`
);
console
.
error
(
`ErrorCode:
${
error
.
code
}
, Message:
${
error
.
message
}
`
);
...
@@ -3680,6 +3704,21 @@ struct WebComponent {
...
@@ -3680,6 +3704,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
### customizeSchemes
static customizeSchemes(schemes: Array
\<
WebCustomScheme
\>
): void
static customizeSchemes(schemes: Array
\<
WebCustomScheme
\>
): void
...
...
Miykael_xxm
🚴
@xiongjiamu
mentioned in commit
c1d1b173
·
4月 18, 2023
mentioned in commit
c1d1b173
mentioned in commit c1d1b17340321db0f2b47f086fcf4238315f058a
开关提交列表
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录