8000 fix: context proxy · dung13890/vue-function-api@10bda45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10bda45

Browse files
committed
fix: context proxy
1 parent f6c85d9 commit 10bda45

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

src/setup.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,28 @@ export function mixin(Vue: VueConstructor) {
8282
'attrs', // confirmed in rfc
8383
// 'listeners', // very likely
8484
];
85-
const method = [
85+
const methodWithoutReturn = [
8686
// 'on', // very likely
8787
// 'once', // very likely
8888
// 'off', // very likely
8989
'emit', // confirmed in rfc
9090
// 'forceUpdate',
9191
// 'destroy'
9292
];
93-
props.forEach(key =>
94-
proxy(ctx, key, () => vm[key], function() {
93+
props.forEach(key => {
94+
proxy(ctx, key, () => vm[`$${key}`], function() {
9595
Vue.util.warn(`Cannot assign to '${key}' because it is a read-only property`, vm);
96+
});
97+
});
98+
methodWithoutReturn.forEach(key =>
99+
proxy(ctx, key, () => {
100+
const vmKey = `$${key}`;
101+
return (...args: any[]) => {
102+
const fn: Function = vm[vmKey];
103+
fn.apply(vm, args);
104+
};
96105
})
97106
);
98-
method.forEach(key => proxy(ctx, key, () => vm[key]));
99-
100107
if (process.env.NODE_ENV === 'test') {
101108
(ctx as any)._vm = vm;
102109
}

src/types/vue.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
import Vue, { ComponentOptions, VNode } from 'vue/';
2-
import { NormalizedScopedSlot } from 'vue/types/vnode';
1+
import Vue, { VNode } from 'vue/';
32

43
export interface Context {
5-
readonly el: Element;
6-
readonly options: ComponentOptions<Vue>;
74
readonly parent: Vue;
85
readonly root: Vue;
9-
readonly children: Vue[];
106
readonly refs: { [key: string]: Vue | Element | Vue[] | Element[] };
117
readonly slots: { [key: string]: VNode[] | undefined };
12-
readonly scopedSlots: { [key: string]: NormalizedScopedSlot | undefined };
13-
readonly isServer: boolean;
14-
readonly ssrContext: any;
15-
readonly vnode: VNode;
16-
readonly attrs: Record<string, string>; //
17-
readonly listeners: Record<string, Function | Function[]>;
8+
readonly attrs: Record<string, string>;
189

19-
on(event: string | string[], callback: Function): this;
20-
once(event: string | string[], callback: Function): this;
21-
off(event?: string | string[], callback?: Function): this;
22-
emit(event: string, ...args: any[]): this;
23-
forceUpdate(): void;
24-
destroy(): void;
10+
emit(event: string, ...args: any[]): void;
2511
}

test/setup.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@ describe('setup', () => {
1111
warn.mockRestore();
1212
});
1313

14+
it('should reveive props as first params', done => {
15+
new Vue({
16+
props: ['a'],
17+
setup(props) {
18+
expect(props.a).toBe(1);
19+
done();
20+
},
21+
propsData: {
22+
a: 1,
23+
},
24+
});
25+
});
26+
27+
it('should reveive context second params', done => {
28+
new Vue({
29+
setup(_, ctx) {
30+
expect(ctx).toBeDefined();
31+
expect('parent' in ctx).toBe(true);
32+
expect(ctx).toEqual(
33+
expect.objectContaining({
34+
root: expect.any(Object),
35+
refs: expect.any(Object),
36+
slots: expect.any(Object),
37+
attrs: expect.any(Object),
38+
emit: expect.any(Function),
39+
})
40+
);
41+
done();
42+
},
43+
});
44+
});
45+
1446
it('warn for exist property(data)', () => {
1547
new Vue({
1648
data() {

0 commit comments

Comments
 (0)
0