Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/uni-mp-toutiao/src/runtime/componentLifetimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
}
} else {
// 如果 vuePid 不存在,则认为当前组件的父是页面
parentVm = instances[webviewId + '_0']
}

if (parentVm) {
relationOptions.parent = parentVm
}
}
}

const props = findPropsData(properties, mpType === 'page')

this.$vm = $createComponent(
Expand Down
Loading