Skip to content

Commit c45e169

Browse files
committed
feat: 增加simple-props组件定义
1 parent a8f5245 commit c45e169

File tree

11 files changed

+465
-229
lines changed

11 files changed

+465
-229
lines changed

example/main.tsx

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,18 @@
11
import '@abraham/reflection'
22
import 'ant-design-vue/dist/reset.css'
3-
import {
4-
Component,
5-
type ComponentProps,
6-
Hook,
7-
injectService,
8-
Link,
9-
mergeRefs,
10-
Mut,
11-
provideService,
12-
VueComponent,
13-
} from 'vue3-oop'
14-
import { createApp, defineComponent, ref, shallowRef } from 'vue'
3+
import { Component, Hook, Link, mergeRefs, Mut, VueComponent } from 'vue3-oop'
4+
import { createApp, shallowRef } from 'vue'
155
import { ConfigProvider, Layout, Menu } from 'ant-design-vue'
166
import { RouterLink, RouterView } from 'vue-router'
177
import { RouterStartService } from './router'
188
import { routes } from './router/routes'
199
import zhCN from 'ant-design-vue/lib/locale/zh_CN'
2010
import { setup } from './setup'
2111

22-
class AService {
23-
height = ref(0)
24-
}
25-
26-
const A1 = defineComponent(() => {
27-
provideService(new AService())
28-
return () => (
29-
<div>
30-
<A2></A2>
31-
</div>
32-
)
33-
})
34-
35-
const A2 = defineComponent(() => {
36-
const a = injectService(AService)
37-
console.log(11111, a)
38-
return () => <div>111 {a.height.value}</div>
39-
})
40-
41-
interface ChildProps {
42-
value?: string
43-
'onUpdate:value'?: (val: string) => any
44-
}
45-
46-
class Child extends VueComponent<ChildProps> {
47-
static defaultProps: ComponentProps<ChildProps> = ['value', 'onUpdate:value']
48-
@Hook('Mounted')
49-
mounted() {
50-
console.log('child mounted')
51-
}
52-
render() {
53-
console.log('child render')
54-
return <div>child</div>
55-
}
56-
}
57-
5812
@Component({
5913
providers: [RouterStartService],
6014
})
6115
class App extends VueComponent {
62-
constructor(private a: RouterStartService) {
63-
super()
64-
}
65-
6616
@Mut() collapsed = false
6717

6818
cc = shallowRef()
@@ -93,11 +43,6 @@ class App extends VueComponent {
9343
>
9444
VUE 示例
9545
</h2>
96-
<Child
97-
ref={(value, refs) => {
98-
console.log(value, refs)
99-
}}
100-
></Child>
10146
<Menu theme={'dark'} mode={'inline'}>
10247
{routes.map((r) => {
10348
return (
@@ -122,7 +67,6 @@ class App extends VueComponent {
12267
</Layout.Sider>
12368
<Layout.Content>
12469
<RouterView></RouterView>
125-
<A1></A1>
12670
</Layout.Content>
12771
</Layout>
12872
</ConfigProvider>

example/module/basic/basic.module.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default class BasicModule extends VueComponent {
1414
{({ Component }: { Component: any }) => {
1515
return (
1616
<div>
17-
<h2>111</h2>
17+
<h2>Suspense容器</h2>
1818
<Suspense
1919
v-slots={{
2020
default: () => [Component],

example/module/basic/basic.router.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ const routes: RouteRecordRaw = {
44
path: '/basic',
55
component: () => import('./basic.module'),
66
meta: {
7-
title: '基础功能',
7+
title: '特色功能',
88
},
99
children: [
1010
{
11-
path: '/basic/hello-world',
12-
component: () => import('./hello-world/hello-world.view'),
11+
path: '/basic/simple-component',
12+
component: () => import('./simple-component/index.view'),
1313
meta: {
14-
title: '加减',
14+
title: '简单组件',
1515
},
1616
},
1717
{
18-
path: '/basic/user-input',
18+
path: '/basic/class-component',
1919
component: () => import('./user-input/user-input.view'),
2020
meta: {
21-
title: '增删改查',
21+
title: '类组件',
2222
},
2323
},
2424
{
@@ -28,6 +28,20 @@ const routes: RouteRecordRaw = {
2828
title: '高阶组件',
2929
},
3030
},
31+
{
32+
path: '/basic/service',
33+
component: () => import('./hoc/hoc.view'),
34+
meta: {
35+
title: '简单服务',
36+
},
37+
},
38+
{
39+
path: '/basic/inject',
40+
component: () => import('./hoc/hoc.view'),
41+
meta: {
42+
title: '复杂服务注入',
43+
},
44+
},
3145
],
3246
}
3347

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { defineComponent, useClassAndStyle } from 'vue3-oop'
2+
import { AllowedComponentProps } from '@/type'
3+
import { ref } from 'vue'
4+
5+
// region 函数组件
6+
export interface SimpleFuncComponentProps extends AllowedComponentProps {
7+
count?: number
8+
}
9+
export function SimpleFuncComponent(props: SimpleFuncComponentProps) {
10+
return <div>函数组件:{props.count}</div>
11+
}
12+
// endregion
13+
14+
// region 状态带属性组件
15+
export interface SimpleStateComponentProps {
16+
initialValue?: number
17+
}
18+
19+
export const SimpleStateComponent = defineComponent(
20+
function SimpleStateComponent(props: SimpleStateComponentProps) {
21+
const classAndStyle = useClassAndStyle()
22+
const count = ref(props.initialValue || 0)
23+
return () => (
24+
<div {...classAndStyle}>
25+
<input type={'number'} v-model={count.value} />
26+
</div>
27+
)
28+
},
29+
)
30+
31+
export const SimpleStateWithDefaultValueComponent = defineComponent(
32+
function SimpleStateWithDefaultValueComponent(
33+
props: SimpleStateComponentProps,
34+
) {
35+
const classAndStyle = useClassAndStyle()
36+
const count = ref(props.initialValue || 0)
37+
return () => (
38+
<div {...classAndStyle}>
39+
<h3>带默认属性参数的组件</h3>
40+
<input type={'number'} v-model={count.value} />
41+
</div>
42+
)
43+
},
44+
{
45+
props: {
46+
initialValue: {
47+
type: Number,
48+
default: 20,
49+
},
50+
},
51+
},
52+
)
53+
54+
// endregion
55+
56+
// 简单状态组件定义
57+
const SimpleComponent = defineComponent(() => {
58+
return () => (
59+
<div>
60+
<h2>简单组件定义</h2>
61+
<h3>函数组件</h3>
62+
<SimpleFuncComponent count={20}></SimpleFuncComponent>
63+
<SimpleStateComponent initialValue={10}></SimpleStateComponent>
64+
<SimpleStateWithDefaultValueComponent></SimpleStateWithDefaultValueComponent>
65+
</div>
66+
)
67+
})
68+
69+
export default SimpleComponent

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"vite-tsconfig-paths": "^4.2.0",
8282
"vitepress": "1.0.0-rc.10",
8383
"vitest": "^0.34.3",
84-
"vue": "^3.3.4",
84+
"vue": "^3.5.13",
8585
"vue-router": "^4.2.4"
8686
},
8787
"commitlint": {

0 commit comments

Comments
 (0)