Skip to content

Commit 2593174

Browse files
committed
文档: 完善文档
1 parent cb08e8f commit 2593174

File tree

12 files changed

+683
-86
lines changed

12 files changed

+683
-86
lines changed

docs/.vitepress/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { UserConfig } from 'vitepress'
22
// @ts-ignore
33
import codetabs from 'markdown-it-codetabs'
4+
import type MarkdownIt from 'markdown-it'
45

56
const config: UserConfig = {
67
base: '/vue3-oop/',
78
title: 'VUE3-OOP',
89
description: 'vue3 oop是vue3开发进入面向对象阶段',
910
markdown: {
1011
lineNumbers: false,
11-
config(md) {
12+
config(md: MarkdownIt) {
1213
md.use(codetabs)
1314
},
1415
},
@@ -32,16 +33,15 @@ const config: UserConfig = {
3233
{ text: '使用指南', link: '/guide/' },
3334
{ text: '组件', link: '/guide/component' },
3435
{ text: '服务', link: '/guide/service' },
35-
{ text: '装饰器', link: '/guide/decorators' },
3636
],
3737
},
3838
{
3939
text: '依赖注入',
4040
children: [{ text: '服务注入', link: '/guide/di' }],
4141
},
4242
{
43-
text: '类型',
44-
children: [{ text: '类型帮助', link: '/guide/type' }],
43+
text: 'API',
44+
link: '/guide/api',
4545
},
4646
],
4747
},

docs/.vitepress/theme/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
import Theme from 'vitepress/theme'
2+
import './theme.scss'
13
import './codetabs.scss'
2-
import theme from 'vitepress/dist/client/theme-default'
34

4-
export default theme
5+
export default Theme

docs/.vitepress/theme/theme.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$color: #16a8a7;
2+
3+
:root {
4+
--c-brand: #{$color};
5+
--c-brand-light: #{lighten($color, 5%)};
6+
}

docs/guide/api.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# 基础类
2+
3+
## VueComponent
4+
5+
可传入泛型 `VueComponent<Props>`
6+
7+
组件必须继承的类,并且必须实现`render`函数
8+
9+
## VueService
10+
11+
服务必须继承的类
12+
13+
# 装饰器
14+
15+
## Ref
16+
- Type: 属性装饰器
17+
18+
声明变量为响应式
19+
20+
```tsx
21+
class Foo extends VueComponent {
22+
@Ref() count = 1
23+
}
24+
```
25+
26+
## Computed
27+
- Type: 存取器属性装饰器
28+
29+
计算属性,加了一层缓存
30+
31+
```tsx
32+
class Foo extends VueComponent {
33+
@Ref() count = 1
34+
35+
@Computed()
36+
get doubleCount() {
37+
return this.count * 2
38+
}
39+
}
40+
```
41+
42+
## Hook
43+
- Type: 方法装饰器
44+
- 'BeforeMount' | 'Mounted' | 'BeforeUpdate' | 'Updated' | 'BeforeUnmount' | 'Unmounted' | 'Activated' | 'Deactivated' | 'ErrorCaptured' | 'RenderTracked' | 'RenderTriggered' | 'ServerPrefetch'
45+
46+
生命周期
47+
48+
```tsx
49+
class Foo extends VueComponent {
50+
@Hook('Mounted')
51+
mount() {
52+
console.log('mount')
53+
}
54+
}
55+
```
56+
57+
## Link
58+
- Type: 属性装饰器
59+
60+
链接到组件或者元素
61+
62+
```tsx
63+
class Foo extends VueComponent {
64+
@Link() div?: HTMLDivElement
65+
66+
render() {
67+
return <div ref="div"></div>
68+
}
69+
}
70+
```
71+
72+
## Autobind
73+
- Type: 类装饰器或者方法装饰器
74+
75+
自动绑定方法的this
76+
77+
```tsx
78+
// 可以直接绑定一个类,类下面的所有方法都会自动绑定
79+
@Autobind()
80+
class Foo extends VueComponent {
81+
@Autobind() // 可单独绑定某个方法
82+
add() {
83+
84+
}
85+
86+
render() {
87+
return <div ref="div"></div>
88+
}
89+
}
90+
```
91+
92+
# 帮助函数
93+
94+
## useProps
95+
96+
获取当前组件的属性
97+
98+
## useCtx
99+
100+
获取当前组件的上下文
101+
102+
## getCurrentApp
103+
104+
获取当前的应用app
105+
106+
# 类型
107+
108+
## ComponentProps
109+
110+
定义组件的props时使用
111+
112+
```tsx
113+
interface FooProps {
114+
size: 'small' | 'large'
115+
}
116+
class Foo extends VueComponent<FooProps> {
117+
static defaultProps: ComponentProps<FooProps> = ['size']
118+
119+
render() {
120+
return <div>{this.props.size}</div>
121+
}
122+
}
123+
```
124+
125+
126+

docs/guide/component.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,147 @@ class Foo extends VueComponent {
1313
}
1414
}
1515
```
16+
17+
## 属性
18+
19+
属性使用接口定义,在组件的 `props` 上面获取
20+
21+
```tsx
22+
import { VueComponent, ComponentProps } from 'vue3-oop'
23+
24+
interface Foo_Props {
25+
size: 'small' | 'large'
26+
}
27+
28+
class Foo extends VueComponent<Foo_Props> {
29+
static defaultProps: ComponentProps<Foo_Props> = ['size']
30+
render() {
31+
return <div>{this.props.size}</div>
32+
}
33+
}
34+
35+
// 泛型组件
36+
interface Bar_Props<T = any> {
37+
data: T
38+
onChange: (item: T) => void
39+
}
40+
class Bar<T> extends VueComponent<Bar_Props<T>> {
41+
static defaultProps: ComponentProps<Bar_Props> = ['data', 'onChange']
42+
render() {
43+
return <div
44+
onClick={() => this.props.onChange?.(this.props.data)}
45+
>{this.props.data}</div>
46+
}
47+
}
48+
49+
```
50+
51+
::: warning 注意!
52+
定义属性之后一定要在类的静态属性 `defaultProps` 定义 vue 需要的属性定义
53+
:::
54+
55+
## 上下文
56+
57+
组件的 `context` 属性上面存储这个组件的 `emit`, `slots`, `attrs`, `expose`,
58+
就是setup函数的第二个参数
59+
60+
```tsx
61+
import { VueComponent } from './component'
62+
63+
class Foo extends VueComponent {
64+
static inheritAttrs = false
65+
66+
render() {
67+
return <div {...this.context.attrs}>foo</div>
68+
}
69+
}
70+
```
71+
72+
## 响应式变量
73+
74+
响应式变量使用装饰器注解一下,主要有2个 `Ref``Computed`,此时忘记 `.value` 的事情,
75+
就是正常普通的变量定义,加上装饰器就是告诉框架当此变量变化的时候我要刷新视图
76+
77+
```tsx
78+
class Foo extends VueComponent {
79+
@Ref() count = 1
80+
81+
@Computed()
82+
get doubleCount() {
83+
return this.count * 2
84+
}
85+
86+
render() {
87+
return (
88+
<div onClick={() => this.count++}>
89+
{this.count}
90+
</div>
91+
)
92+
}
93+
}
94+
```
95+
96+
## 生命周期
97+
98+
生命周期使用 `Hook` 装饰器
99+
100+
```tsx
101+
class Foo extends VueComponent {
102+
@Hook('Mounted')
103+
mounted() {
104+
console.log('foo mounted')
105+
}
106+
107+
render() {
108+
return <span>foo</span>
109+
}
110+
}
111+
```
112+
113+
## watch
114+
115+
watch在构造函数中使用, 构造函数其实就认为是 setup,你可以做任何在setup中使用的方法
116+
117+
```tsx
118+
import { watch } from 'vue'
119+
120+
class Foo extends VueComponent {
121+
constructor() {
122+
super()
123+
watch(() => this.count, (n, o) => console.log('change', n, o))
124+
}
125+
126+
@Ref() count = 1
127+
128+
render() {
129+
return <span onClick={() => this.count++}>{this.count}</span>
130+
}
131+
}
132+
```
133+
134+
## 插槽
135+
136+
slots本质上其实是属性的一部分,为了模板的需要单独给拿出来,他其实就是类似于 `react` 中的 `renderProps`,
137+
所以我们可以在属性定义的时候定义一下,
138+
139+
```tsx
140+
import { VNodeChild } from 'vue'
141+
import { VueComponent } from 'vue3-oop'
142+
143+
interface Foo_Props {
144+
slots: {
145+
item(name: string): VNodeChild
146+
}
147+
}
148+
149+
// 此时如果只有slots的话就可以不用定义 defaultProps
150+
class Foo extends VueComponent<Foo_Props> {
151+
render() {
152+
return (
153+
<div>
154+
{this.context.slots.item?.('aaaa')}
155+
</div>
156+
)
157+
}
158+
}
159+
```

0 commit comments

Comments
 (0)