Skip to content

Commit 05785cf

Browse files
committed
优化note中文档
1 parent 4f0a048 commit 05785cf

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

docs/note/01.父组件与子组件的理解.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
比如在用户点击某个按钮时,在事件处理回调函数中可以调用 findService 实时查询子组件的服务,此时是通过业务逻辑来保证子组件一定是存在的。
2828
当然也可以不存在,只时需要做判空处理。
2929

30-
需要注意`findService(token, component)`需要 2 个参数,第 1 个参数是服务的 token,第 2 个参数是当前组件的实例。
31-
相比于`useService(token)`多1个参数,需要明确手动传递当前组件的实例对象。
30+
需要注意`findService(token)`需要 1 个参数,这个参数是子组件服务的 token。
3231

3332
## 子组件获取父组件的服务
3433

@@ -37,7 +36,7 @@
3736

3837
在子组件的实例化阶段,可以直接使用 useService 直接获取父组件的服务。
3938

40-
需要注意`useService(token)`只需要 1 个参数,就是父组件服务的 token,但是`useService`需要依赖`getCurrentInstance``hasInjectionContext`,也就是`useService`只能在 setup 环境下使用。
39+
需要注意`useService(token)`需要 1 个参数,这个参数是父组件服务的 token,但是`useService`需要依赖`getCurrentInstance``hasInjectionContext`,也就是`useService`只能在 setup 环境下使用。
4140

4241
## 总结
4342

docs/note/03.父组件获取子组件服务.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ findService 负责从当前组件的子组件/子孙组件中查找指定的服
1313

1414
## 实现方案
1515

16-
可选的方案有如下 3 种:
16+
理论上可选的方案有如下 4 种:
1717

1818
方案 1:
1919

@@ -36,7 +36,16 @@ this.component.getServiceFromChild(ChildServiceToken);
3636
getServiceFromChild(ChildServiceToken, this.component);
3737
```
3838

39-
最终确定的实现方案是方案 3,也就是`findService(token, component)`
39+
方案 4:
40+
41+
```ts
42+
// 这里的FIND_CHILD_SERVICE是本库提供的token
43+
const findService = useService(FIND_CHILD_SERVICE);
44+
const service = findService(ChildServiceToken);
45+
```
46+
47+
最终确定的实现方案是方案 4,也就是首先利用 useService 获取 findService 工具方法,然后调用 findService 方法查找子组件的服务。
48+
这个访问的优点是不需要暴露 container 和 component,而且可以同时在组件内使用和服务中使用。
4049

4150
实现逻辑可以参考这里[vuejs/test-utils#findComponent](https://github.com/vuejs/test-utils/blob/9c9659441c59de557f5844e5f9b7fee00b3938e0/src/baseWrapper.ts#L154)
4251

@@ -47,12 +56,12 @@ getServiceFromChild(ChildServiceToken, this.component);
4756
```vue
4857
<script lang="ts" setup>
4958
import { getCurrentInstance } from 'vue';
50-
import { findService, Token } from '@kaokei/use-vue-service';
59+
import { FIND_CHILD_SERVICE, Token, useService } from '@kaokei/use-vue-service';
5160
const token = new Token<ChildServiceType>('child service name');
52-
const component = getCurrentInstance();
5361
5462
function handleClickEvent() {
55-
const childService = findService(token, component);
63+
const findService = useService(FIND_CHILD_SERVICE);
64+
const childService = findService(token);
5665
childService.doSomthing();
5766
}
5867
</script>
@@ -61,16 +70,13 @@ function handleClickEvent() {
6170
场景 1:在服务中使用
6271

6372
```ts
64-
import {
65-
Inject,
66-
findService,
67-
Token,
68-
} from '@kaokei/use-vue-service';
73+
import { Inject, FIND_CHILD_SERVICE, Token, useService } from '@kaokei/use-vue-service';
6974
const token = new Token<ChildServiceType>('child service name');
7075

7176
class DemoService {
7277
public handleClickEvent() {
73-
const childService = findService(token, component);
78+
const findService = useService(FIND_CHILD_SERVICE);
79+
const childService = findService(token);
7480
childService.doSomthing();
7581
}
7682
}

0 commit comments

Comments
 (0)