-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(mp-toutiao): 修复组合式父子组件 inject 失效的问题 #5945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,12 +18,14 @@ import { | |||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| $createComponent, | ||||||||||||||||||||||||||||||||||||
| $destroyComponent, | ||||||||||||||||||||||||||||||||||||
| findVmByVueId, | ||||||||||||||||||||||||||||||||||||
| initComponentInstance, | ||||||||||||||||||||||||||||||||||||
| initMocks, | ||||||||||||||||||||||||||||||||||||
| initRefs, | ||||||||||||||||||||||||||||||||||||
| initVueIds, | ||||||||||||||||||||||||||||||||||||
| } from '@dcloudio/uni-mp-core' | ||||||||||||||||||||||||||||||||||||
| import { ON_READY } from '@dcloudio/uni-shared' | ||||||||||||||||||||||||||||||||||||
| import { instances } from './parseComponentOptions' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const fixAttached = __PLATFORM__ === 'mp-toutiao' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -57,6 +59,30 @@ export function initLifetimes({ | |||||||||||||||||||||||||||||||||||
| mpInstance.route = mpInstance.__route__ | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (__PLATFORM__ === 'mp-toutiao') { | ||||||||||||||||||||||||||||||||||||
| // 在创建 Vue 组件实例之前,先查找父组件,以支持组合式 API 的 inject | ||||||||||||||||||||||||||||||||||||
| if (mpType === 'component') { | ||||||||||||||||||||||||||||||||||||
| const webviewId = mpInstance.__webviewId__ + '' | ||||||||||||||||||||||||||||||||||||
| const vuePid = relationOptions.vuePid | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let parentVm | ||||||||||||||||||||||||||||||||||||
| if (vuePid) { | ||||||||||||||||||||||||||||||||||||
| // 尝试从页面查找父组件 | ||||||||||||||||||||||||||||||||||||
| const pageVm = instances[webviewId + '_0'] as ComponentPublicInstance | ||||||||||||||||||||||||||||||||||||
| if (pageVm) { | ||||||||||||||||||||||||||||||||||||
| parentVm = findVmByVueId(pageVm, vuePid) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+70
to
+73
|
||||||||||||||||||||||||||||||||||||
| // 尝试从页面查找父组件 | |
| const pageVm = instances[webviewId + '_0'] as ComponentPublicInstance | |
| if (pageVm) { | |
| parentVm = findVmByVueId(pageVm, vuePid) | |
| // 通过 instances 查找父组件,避免依赖尚未填充的 $children | |
| const instanceList = Object.values(instances) as ComponentPublicInstance[] | |
| for (let i = 0; i < instanceList.length; i++) { | |
| const vm = instanceList[i] as any | |
| const scope = vm && vm.$scope | |
| if ( | |
| scope && | |
| scope._$vueId === vuePid && | |
| (scope.__webviewId__ + '') === webviewId | |
| ) { | |
| parentVm = vm | |
| break | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix only applies to
mp-toutiao, but the sharedinitRelationfunction inparseComponentOptions.ts(line 53) already recognizes thatmp-larksuffers from the same latetriggerEventtiming issue. Sincemp-larkreuses this samecomponentLifetimes.tsfile (viapackages/uni-mp-lark/src/runtime/index.ts), the inject fix should also covermp-lark. The guard should be__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark'to be consistent.