Skip to content

Commit e531517

Browse files
author
lishiwen
committed
feat:
1 parent e55d75a commit e531517

File tree

5 files changed

+110
-11
lines changed

5 files changed

+110
-11
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createTextVNode, h, useInject } from "../../lib/m-vue.esm.js";
2+
3+
export default {
4+
name: "Consumer",
5+
render() {
6+
return h("div", {}, [
7+
createTextVNode(
8+
`自定义增强的inject: ${this.provideContext.count} - ${this.provideContext.notFound}`
9+
),
10+
]);
11+
},
12+
13+
setup() {
14+
const provideContext = useInject(["two", "count", "notFound"], {
15+
notFound: "this is default value",
16+
});
17+
return {
18+
provideContext,
19+
};
20+
},
21+
};

example/provide-inject/ProvideTwo.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { createTextVNode, h, provide, inject } from "../../lib/m-vue.esm.js";
22
import Consumer from "./Consumer.js";
3+
import EnhanceInject from "./EnhanceInject.js";
34

45
export default {
56
name: "ProviderTwo",
67

78
render() {
8-
return h("div", {}, [h("div", {}, "bar: " + this.bar), h(Consumer)]);
9+
return h("div", {}, [
10+
h("div", {}, "bar: " + this.bar),
11+
h(Consumer),
12+
h(EnhanceInject),
13+
]);
914
},
1015

1116
setup() {

lib/m-vue.cjs.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,37 @@ function provide(key, value) {
429429
provides[key] = value;
430430
}
431431
}
432-
function inject(key) {
432+
function inject(key, defaultValue) {
433433
const currentInstance = getCurrentInstance();
434434
if (currentInstance) {
435435
const parentProvides = currentInstance.parent.provides;
436-
console.log(key, parentProvides);
437-
return parentProvides[key];
436+
if (key in parentProvides) {
437+
return parentProvides[key];
438+
}
439+
else if (defaultValue) {
440+
return defaultValue;
441+
}
442+
}
443+
}
444+
// 扩展的
445+
function useInject(keys = [], defaultValues = {}) {
446+
const currentInstance = getCurrentInstance();
447+
if (currentInstance) {
448+
const parentProvides = currentInstance.parent.provides;
449+
return composeInjectValues(parentProvides, keys, defaultValues);
438450
}
451+
return {};
452+
}
453+
function composeInjectValues(provides = {}, keys = [], defaultValues = {}) {
454+
return keys.reduce((result, currentKey) => {
455+
if (currentKey in provides) {
456+
result[currentKey] = provides[currentKey];
457+
}
458+
else if (defaultValues[currentKey]) {
459+
result[currentKey] = defaultValues[currentKey];
460+
}
461+
return result;
462+
}, {});
439463
}
440464

441465
exports.clearCurrentInstance = clearCurrentInstance;
@@ -452,3 +476,4 @@ exports.render = render;
452476
exports.renderSlots = renderSlots;
453477
exports.setCurrentInstance = setCurrentInstance;
454478
exports.setupComponent = setupComponent;
479+
exports.useInject = useInject;

lib/m-vue.esm.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,37 @@ function provide(key, value) {
425425
provides[key] = value;
426426
}
427427
}
428-
function inject(key) {
428+
function inject(key, defaultValue) {
429429
const currentInstance = getCurrentInstance();
430430
if (currentInstance) {
431431
const parentProvides = currentInstance.parent.provides;
432-
console.log(key, parentProvides);
433-
return parentProvides[key];
432+
if (key in parentProvides) {
433+
return parentProvides[key];
434+
}
435+
else if (defaultValue) {
436+
return defaultValue;
437+
}
438+
}
439+
}
440+
// 扩展的
441+
function useInject(keys = [], defaultValues = {}) {
442+
const currentInstance = getCurrentInstance();
443+
if (currentInstance) {
444+
const parentProvides = currentInstance.parent.provides;
445+
return composeInjectValues(parentProvides, keys, defaultValues);
434446
}
447+
return {};
448+
}
449+
function composeInjectValues(provides = {}, keys = [], defaultValues = {}) {
450+
return keys.reduce((result, currentKey) => {
451+
if (currentKey in provides) {
452+
result[currentKey] = provides[currentKey];
453+
}
454+
else if (defaultValues[currentKey]) {
455+
result[currentKey] = defaultValues[currentKey];
456+
}
457+
return result;
458+
}, {});
435459
}
436460

437-
export { clearCurrentInstance, createApp, createComponentInstance, createTextVNode, createVNode, getCurrentInstance, h, inject, patch, provide, render, renderSlots, setCurrentInstance, setupComponent };
461+
export { clearCurrentInstance, createApp, createComponentInstance, createTextVNode, createVNode, getCurrentInstance, h, inject, patch, provide, render, renderSlots, setCurrentInstance, setupComponent, useInject };

src/runtime-core/inject.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,35 @@ export function provide(key, value) {
1919
}
2020
}
2121

22-
export function inject(key) {
22+
export function inject(key, defaultValue) {
2323
const currentInstance: any = getCurrentInstance();
2424
if (currentInstance) {
2525
const parentProvides = currentInstance.parent.provides;
26-
console.log(key, parentProvides)
27-
return parentProvides[key];
26+
if (key in parentProvides) {
27+
return parentProvides[key];
28+
} else if (defaultValue) {
29+
return defaultValue
30+
}
31+
}
32+
}
33+
34+
// 扩展的
35+
export function useInject(keys = [], defaultValues = {}) {
36+
const currentInstance: any = getCurrentInstance();
37+
if (currentInstance) {
38+
const parentProvides = currentInstance.parent.provides;
39+
return composeInjectValues(parentProvides, keys, defaultValues);
2840
}
41+
return {}
42+
}
43+
44+
function composeInjectValues(provides = {}, keys = [], defaultValues = {}) {
45+
return keys.reduce((result, currentKey) => {
46+
if (currentKey in provides) {
47+
result[currentKey] = provides[currentKey];
48+
} else if (defaultValues[currentKey]) {
49+
result[currentKey] = defaultValues[currentKey]
50+
}
51+
return result;
52+
}, {})
2953
}

0 commit comments

Comments
 (0)