Skip to content

Commit 77bc8c6

Browse files
committed
feat: 函数式组件 插槽
1 parent aff3b0b commit 77bc8c6

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/components/Button/Button.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ export default class ZButton extends tsc.Component<ButtonProps> {
3636
}
3737

3838
@api(demoApi)
39-
protected getResult(res?: string) {
39+
protected getResult() {
4040
// 获取api调用结果
41-
console.log(res);
41+
return (res?: string) => {
42+
console.log(res);
43+
};
4244
}
4345

4446
protected render() {
@@ -49,6 +51,8 @@ export default class ZButton extends tsc.Component<ButtonProps> {
4951
return (
5052
<div>
5153
<p class={className}>user: {this.user.name}</p>
54+
{this.$slots.default || "组件默认slot内容"}
55+
{this.$slots.com_name_slot || "组件中的具名slot"}
5256
<van-button type={this.type} onClick={() => this.getResult()}>
5357
{this.text || "我是按钮"}
5458
</van-button>

src/decorator/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: ZhengXiaowei
33
* @Date: 2020-02-25 17:34:04
44
* @LastEditors: ZhengXiaowei
5-
* @LastEditTime: 2020-02-25 19:37:38
5+
* @LastEditTime: 2020-03-05 09:42:51
66
* @Description: 自定义装饰器方法
77
* @FilePath: /src/decorator/index.ts
88
*/
@@ -19,9 +19,9 @@ export const api = (promise: Function) => {
1919
descriptor.value = async function() {
2020
try {
2121
let result = await promise();
22-
oldValue.call(this, result);
22+
oldValue.call(this, ...arguments)(result);
2323
} catch (e) {
24-
oldValue.call(this, {}, e);
24+
oldValue.call(this, ...arguments)({}, e);
2525
}
2626
};
2727
};

src/fun-components/fun-layout.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as tsx from "vue-tsx-support";
2+
3+
interface Props {
4+
msg?: string;
5+
name?: string;
6+
}
7+
8+
export default tsx.componentFactory.create<Props, {}>({
9+
name: "FunLayout",
10+
functional: true,
11+
render(h, ctx) {
12+
return (
13+
<div>
14+
{ctx.props.msg} - {ctx.props.name}
15+
{ctx.slots().default}
16+
{ctx.slots().name_slot || "默认的具名slot内容"}
17+
</div>
18+
);
19+
}
20+
});

src/views/Home.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@ import { Component, Vue } from "vue-property-decorator";
22
import { Component as tsc } from "vue-tsx-support";
33

44
import ZButton, { BType } from "@/components/Button/Button";
5+
import FunLayout from "@/fun-components/fun-layout";
56

67
@Component
78
export default class HomeContainer extends tsc<Vue> {
89
protected render() {
9-
return <ZButton type={BType.primary}></ZButton>;
10+
return (
11+
<div>
12+
<ZButton type={BType.primary}>
13+
<p>替换组件默认slot</p>
14+
<p>替换组件默认slot</p>
15+
<p>替换组件默认slot</p>
16+
<p>替换组件默认slot</p>
17+
<p slot="com_name_slot">替换组件的具名slot</p>
18+
</ZButton>
19+
<FunLayout msg="内容" name="张三">
20+
<p>默认的slot</p>
21+
<p slot="name_slot">具名slot</p>
22+
</FunLayout>
23+
</div>
24+
);
1025
}
1126
}

0 commit comments

Comments
 (0)