Skip to content

fix(mp-toutiao): 修复组合式父子组件 inject 失效的问题 - #5945

Closed
chouchouji wants to merge 2 commits into
nextfrom
fix-mp-toutiao-provide-inject
Closed

fix(mp-toutiao): 修复组合式父子组件 inject 失效的问题#5945
chouchouji wants to merge 2 commits into
nextfrom
fix-mp-toutiao-provide-inject

Conversation

@chouchouji

@chouchouji chouchouji commented Feb 6, 2026

Copy link
Copy Markdown
Member

close #5944

@github-actions

github-actions Bot commented Feb 6, 2026

Copy link
Copy Markdown

Size report

Path Size
packages/size-check/dist/size-check.es.js 48.01 KB (0%)
packages/size-check/dist/style.css 2.82 KB (0%)
packages/uni-app/dist/uni-app.es.js 2.64 KB (0%)
packages/uni-h5-vue/dist/vue.runtime.esm.js 76.48 KB (0%)
packages/uni-mp-vue/dist/vue.runtime.esm.js 43.74 KB (0%)
packages/uni-mp-alipay/dist/uni.api.esm.js 15.05 KB (0%)
packages/uni-mp-alipay/dist/uni.mp.esm.js 8.17 KB (0%)
packages/uni-mp-baidu/dist/uni.api.esm.js 11.33 KB (0%)
packages/uni-mp-baidu/dist/uni.mp.esm.js 9.68 KB (0%)
packages/uni-mp-qq/dist/uni.api.esm.js 10.65 KB (0%)
packages/uni-mp-qq/dist/uni.mp.esm.js 7.6 KB (0%)
packages/uni-mp-toutiao/dist/uni.api.esm.js 10.59 KB (0%)
packages/uni-mp-toutiao/dist/uni.mp.esm.js 9.56 KB (+1.47% 🔺)
packages/uni-mp-weixin/dist/uni.api.esm.js 11.65 KB (0%)
packages/uni-mp-weixin/dist/uni.mp.esm.js 8.16 KB (0%)
packages/uni-quickapp-webview/dist/uni.api.esm.js 10.6 KB (0%)
packages/uni-quickapp-webview/dist/uni.mp.esm.js 8.16 KB (0%)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR attempts to fix the inject (composition API) not working correctly for child components in mp-toutiao (TikTok Mini Program). The root cause is that in mp-toutiao, triggerEvent('__l') fires very late (after ready), meaning parent-child relationships are established too late for inject/provide to work in setup(). The fix adds an early parent-lookup step during attached, before $createComponent is called, so the Vue component can be initialized with its parent already known.

Changes:

  • Imports findVmByVueId and instances into componentLifetimes.ts
  • Adds an early parent resolution block for mp-toutiao components during attached, populating relationOptions.parent before the Vue instance is created

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

mpInstance.route = mpInstance.__route__
}

if (__PLATFORM__ === 'mp-toutiao') {

Copilot AI Mar 4, 2026

Copy link

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 shared initRelation function in parseComponentOptions.ts (line 53) already recognizes that mp-lark suffers from the same late triggerEvent timing issue. Since mp-lark reuses this same componentLifetimes.ts file (via packages/uni-mp-lark/src/runtime/index.ts), the inject fix should also cover mp-lark. The guard should be __PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark' to be consistent.

Copilot uses AI. Check for mistakes.
Comment on lines +70 to +73
// 尝试从页面查找父组件
const pageVm = instances[webviewId + '_0'] as ComponentPublicInstance
if (pageVm) {
parentVm = findVmByVueId(pageVm, vuePid)

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findVmByVueId(pageVm, vuePid) traverses pageVm.$children to find the parent component matching vuePid. However, $children in this platform is only populated inside handleLink (see parseComponentOptions.ts line 119: (parentVm as any).$children.push(vm)), which fires very late — after ready — due to the late triggerEvent timing in mp-toutiao.

This means that at the time this early lookup runs during attached, pageVm.$children is empty (no child component has had handleLink called yet), so findVmByVueId will always return undefined when vuePid is set. The fix therefore only works for direct children of the page (the else branch where vuePid is absent).

For components nested more than one level deep (e.g., Page → ParentComponent → ChildComponent), the parentVm lookup will fail and relationOptions.parent will not be set, leaving the inject issue unfixed for those cases.

Consider an alternative approach: iterating over the instances map values (already populated by parent's initRelation call) and matching by $scope._$vueId === vuePid, rather than relying on $children which is not yet populated.

Suggested change
// 尝试从页面查找父组件
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
}

Copilot uses AI. Check for mistakes.
@lidongtony

Copy link
Copy Markdown

请问官方这个分支什么时候能合并呢?非常期待!

@chouchouji

Copy link
Copy Markdown
Member Author

请问官方这个分支什么时候能合并呢?非常期待!

这个 pr 需要同事 review 下,他目前有其他紧急的事情忙,im 里面私聊我,我发你临时解决方案

@StrivingRabbit
StrivingRabbit deleted the branch next March 19, 2026 08:16
@lidongtony

Copy link
Copy Markdown

怎么关闭了

@StrivingRabbit

Copy link
Copy Markdown
Collaborator

十分抱歉,由于分支名称调整,pr 被关闭,会重新提交 pr,不好意思

@chouchouji

Copy link
Copy Markdown
Member Author

新的 pr #5966

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uniapp,vue3,打包抖音小程序使用inject问题

4 participants