Skip to content

Commit 47110ad

Browse files
committed
@provide and docs
1 parent 02cf7a8 commit 47110ad

File tree

9 files changed

+71
-9
lines changed

9 files changed

+71
-9
lines changed

changelog/v3.0.1.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
1. Use class constructor name as default component name.
44

5-
2. `Ref` decorator accepts `key` paramater.
5+
2. `@Ref` decorator accepts `key` paramater.
6+
7+
3. New `@Provide` decorator
68

79
# Document Update
810

9-
'@Ref', which accepts `key` paramater.
11+
1. `@Ref`, which accepts `key` paramater.
12+
13+
2. New `@Provide` decorator

docs/en/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Event](/en/class-component/event/event.md)
1111
- [Reference](/en/class-component/reference/reference.md)
1212
- [Watcher](/en/class-component/watcher/watcher.md)
13+
- [Provide](/en/class-component/provide/provide.md)
1314
- [Injection](/en/class-component/injection/injection.md)
1415
- [Model](/en/class-component/model/model.md)
1516
- [Setup](/en/class-component/setup/setup.md)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component, Vue, Provide, toNative } from 'vue-facing-decorator'
2+
3+
@Component
4+
class MyComponent extends Vue {
5+
@Provide
6+
p1 = 'foo'
7+
8+
@Provide('alias')
9+
p2 = 'bar'
10+
}
11+
12+
13+
export default toNative(MyComponent)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Usage
2+
3+
This is the vue `Provide` concept.
4+
5+
[](./code-usage-base.ts ':include :type=code typescript')

docs/zh-cn/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [引用](/zh-cn/class-component/reference/reference.md)
1212
- [监视器](/zh-cn/class-component/watcher/watcher.md)
1313
- [注入](/zh-cn/class-component/injection/injection.md)
14+
- [提供](/zh-cn/class-component/provide/provide.md)
1415
- [Model](/zh-cn/class-component/model/model.md)
1516
- [Composition 组件](/zh-cn/class-component/setup/setup.md)
1617
- 继承
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## 用法
2+
3+
和Vue的`Provide`概念相同。
4+
5+
[](../../../en/class-component/provide/code-usage-base.ts ':include :type=code typescript')

src/option/data.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export function build(cons: Cons, optionBuilder: OptionBuilder, vueInstance: any
99
return !!des.enumerable
1010
})
1111
const slot = obtainSlot(cons.prototype)
12-
names = excludeNames(names, slot)
12+
names = excludeNames(names, slot,(mapName) => {
13+
//include these names:
14+
//provide, user may access field directly
15+
return !['provide'].includes(mapName)
16+
})
1317
Object.assign(optionBuilder.data, makeObject(names, sample))
1418
}

src/option/methodsAndHooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
5656
//watch, user may call watch method directly
5757
//hooks, user may call hook method directly
5858
//emits, user may have a method name which is same as one of event names
59-
return !['watch', 'hooks', 'emits'].includes(mapName)
59+
return !['watch', 'hooks', 'emits', 'provide'].includes(mapName)
6060
});
6161
names.forEach(name => {
6262
if (HookNames.includes(name as any) || map.has(name)) {
@@ -72,7 +72,7 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
7272
})
7373

7474
Object.assign(optionBuilder.methods, MethodFunctions)
75-
const beforeCreateCallbacks = [...optionBuilder.beforeCreateCallbacks??[]]
75+
const beforeCreateCallbacks = [...optionBuilder.beforeCreateCallbacks ?? []]
7676
if (beforeCreateCallbacks && beforeCreateCallbacks.length > 0) {
7777
const oldBeforeCreateCallback = HookFunctions['beforeCreate']
7878
HookFunctions['beforeCreate'] = function () {

test/option/provide.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
import { expect } from 'chai';
33
import 'mocha';
4-
import { Component, Provide, Base, Inject } from '../../dist'
5-
import {VueWrapper, mount} from '@vue/test-utils';
4+
import { Component, Provide, Base, Inject, toNative } from '../../dist'
5+
import { VueWrapper, mount } from '@vue/test-utils';
66

77
describe('decorator Provide',
88
() => {
@@ -23,7 +23,7 @@ describe('decorator Provide',
2323
readonly _internalName = 123
2424
}
2525

26-
@Component({template: '<span />'})
26+
@Component({ template: '<span />' })
2727
class Child extends Base {
2828
@Inject
2929
readonly fromClassDecorator!: string
@@ -84,7 +84,7 @@ describe('decorator Provide',
8484
readonly ambiguous = 'property'
8585
}
8686

87-
@Component({template: '<span />'})
87+
@Component({ template: '<span />' })
8888
class Child extends Base {
8989
@Inject
9090
readonly ambiguous!: string
@@ -105,5 +105,34 @@ describe('decorator Provide',
105105

106106
expect(child.vm.ambiguous).to.equal('class')
107107
})
108+
109+
it('preserve feild', () => {
110+
@Component({
111+
template: '<div><slot/></div>',
112+
})
113+
class Comp extends Base {
114+
@Provide
115+
readonly vp = 'foo'
116+
v!: string
117+
118+
@Provide
119+
fp() { }
120+
f!: Function
121+
mounted() {
122+
123+
this.v = this.vp
124+
this.f = this.fp
125+
126+
}
127+
128+
129+
}
130+
131+
132+
const component = mount(toNative(Comp) as any)
133+
134+
expect(component.vm.v).to.equal('foo')
135+
expect(typeof component.vm.f).to.equal('function')
136+
})
108137
}
109138
)

0 commit comments

Comments
 (0)