]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): prioritize using the provides from currentApp in nested createApp...
authoredison <daiwei521@126.com>
Wed, 7 Aug 2024 04:02:38 +0000 (12:02 +0800)
committerGitHub <noreply@github.com>
Wed, 7 Aug 2024 04:02:38 +0000 (12:02 +0800)
close #11488

packages/runtime-core/__tests__/apiCreateApp.spec.ts
packages/runtime-core/src/apiInject.ts

index f638633974132d3eef367f72d75d30c7e9ace141..23843b59993018009e847c67fcd6fb111f9c75e0 100644 (file)
@@ -116,12 +116,25 @@ describe('api: createApp', () => {
     const app = createApp({
       setup() {
         provide('foo', 'should not be seen')
+
+        // nested createApp
+        const childApp = createApp({
+          setup() {
+            provide('foo', 'foo from child')
+          },
+        })
+
+        childApp.provide('foo', 2)
+        expect(childApp.runWithContext(() => inject('foo'))).toBe(2)
+
         return () => h('div')
       },
     })
     app.provide('foo', 1)
 
     expect(app.runWithContext(() => inject('foo'))).toBe(1)
+    const root = nodeOps.createElement('div')
+    app.mount(root)
 
     expect(
       app.runWithContext(() => {
index f15983604bb3c2b78607f6cbba27a906a0331023..2021f6d3177502318dd1e8fd1c49065cda9e218c 100644 (file)
@@ -56,11 +56,14 @@ export function inject(
     // #2400
     // to support `app.use` plugins,
     // fallback to appContext's `provides` if the instance is at root
-    const provides = instance
-      ? instance.parent == null
-        ? instance.vnode.appContext && instance.vnode.appContext.provides
-        : instance.parent.provides
-      : currentApp!._context.provides
+    // #11488, in a nested createApp, prioritize using the provides from currentApp
+    const provides = currentApp
+      ? currentApp._context.provides
+      : instance
+        ? instance.parent == null
+          ? instance.vnode.appContext && instance.vnode.appContext.provides
+          : instance.parent.provides
+        : undefined
 
     if (provides && (key as string | symbol) in provides) {
       // TS doesn't allow symbol as index type