Skip to content

Commit

Permalink
fix(runtime): 优化暴露给开发者的页面路由参数,close #9414
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Jun 1, 2021
1 parent bea327f commit 7a784ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
76 changes: 31 additions & 45 deletions packages/taro-runtime/src/dsl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,30 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco

Current.page = this as any
this.config = pageConfig || {}
if (this.options == null) {
this.options = options
options.$taroTimestamp = Date.now()

// this.$taroPath 是页面唯一标识,不可变,因此页面参数 options 也不可变
this.$taroPath = getPath(id, options)
// this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
if (this.$taroParams == null) {
this.$taroParams = Object.assign({}, options)
}
this.options.$taroTimestamp = Date.now()

const path = getPath(id, this.options)
const router = isBrowser ? path : this.route || this.__route__
const router = isBrowser ? this.$taroPath : this.route || this.__route__
Current.router = {
params: this.options,
params: this.$taroParams,
path: addLeadingSlash(router),
onReady: getOnReadyEventKey(id),
onShow: getOnShowEventKey(id),
onHide: getOnHideEventKey(id)
}

const mount = () => {
Current.app!.mount!(component, path, () => {
pageElement = document.getElementById<TaroRootElement>(path)
Current.app!.mount!(component, this.$taroPath, () => {
pageElement = document.getElementById<TaroRootElement>(this.$taroPath)

ensure(pageElement !== null, '没有找到页面实例。')
safeExecute(path, 'onLoad', this.options)
safeExecute(this.$taroPath, 'onLoad', this.$taroParams)
if (!isBrowser) {
pageElement.ctx = this
pageElement.performUpdate(true, cb)
Expand All @@ -136,21 +139,18 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
}
},
onReady () {
const path = getPath(id, this.options)

raf(() => {
eventCenter.trigger(getOnReadyEventKey(id))
})

safeExecute(path, 'onReady')
safeExecute(this.$taroPath, 'onReady')
this.onReady.called = true
},
onUnload () {
const path = getPath(id, this.options)
unmounting = true
Current.app!.unmount!(path, () => {
Current.app!.unmount!(this.$taroPath, () => {
unmounting = false
instances.delete(path)
instances.delete(this.$taroPath)
if (pageElement) {
pageElement.ctx = null
}
Expand All @@ -163,10 +163,9 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
onShow () {
Current.page = this as any
this.config = pageConfig || {}
const path = getPath(id, this.options)
const router = isBrowser ? path : this.route || this.__route__
const router = isBrowser ? this.$taroPath : this.route || this.__route__
Current.router = {
params: this.options,
params: this.$taroParams,
path: addLeadingSlash(router),
onReady: getOnReadyEventKey(id),
onShow: getOnShowEventKey(id),
Expand All @@ -177,54 +176,43 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
eventCenter.trigger(getOnShowEventKey(id))
})

safeExecute(path, 'onShow')
safeExecute(this.$taroPath, 'onShow')
},
onHide () {
Current.page = null
Current.router = null
const path = getPath(id, this.options)
safeExecute(path, 'onHide')
safeExecute(this.$taroPath, 'onHide')
eventCenter.trigger(getOnHideEventKey(id))
},
onPullDownRefresh () {
const path = getPath(id, this.options)
return safeExecute(path, 'onPullDownRefresh')
return safeExecute(this.$taroPath, 'onPullDownRefresh')
},
onReachBottom () {
const path = getPath(id, this.options)
return safeExecute(path, 'onReachBottom')
return safeExecute(this.$taroPath, 'onReachBottom')
},
onPageScroll (options) {
const path = getPath(id, this.options)
return safeExecute(path, 'onPageScroll', options)
return safeExecute(this.$taroPath, 'onPageScroll', options)
},
onResize (options) {
const path = getPath(id, this.options)
return safeExecute(path, 'onResize', options)
return safeExecute(this.$taroPath, 'onResize', options)
},
onTabItemTap (options) {
const path = getPath(id, this.options)
return safeExecute(path, 'onTabItemTap', options)
return safeExecute(this.$taroPath, 'onTabItemTap', options)
},
onTitleClick () {
const path = getPath(id, this.options)
return safeExecute(path, 'onTitleClick')
return safeExecute(this.$taroPath, 'onTitleClick')
},
onOptionMenuClick () {
const path = getPath(id, this.options)
return safeExecute(path, 'onOptionMenuClick')
return safeExecute(this.$taroPath, 'onOptionMenuClick')
},
onPopMenuClick () {
const path = getPath(id, this.options)
return safeExecute(path, 'onPopMenuClick')
return safeExecute(this.$taroPath, 'onPopMenuClick')
},
onPullIntercept () {
const path = getPath(id, this.options)
return safeExecute(path, 'onPullIntercept')
return safeExecute(this.$taroPath, 'onPullIntercept')
},
onAddToFavorites () {
const path = getPath(id, this.options)
return safeExecute(path, 'onAddToFavorites')
return safeExecute(this.$taroPath, 'onAddToFavorites')
}
}

Expand All @@ -241,16 +229,14 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
options.target!.dataset = element.dataset
}
}
const path = getPath(id, this.options)
return safeExecute(path, 'onShareAppMessage', options)
return safeExecute(this.$taroPath, 'onShareAppMessage', options)
}
}
if (component.onShareTimeline ||
component.prototype?.onShareTimeline ||
component.enableShareTimeline) {
config.onShareTimeline = function () {
const path = getPath(id, this.options)
return safeExecute(path, 'onShareTimeline')
return safeExecute(this.$taroPath, 'onShareTimeline')
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/taro-runtime/src/hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export interface MpInstance {
setData: (data: unknown, cb: () => void) => void;
route?: string;
__route__: string;
options?: Record<string, unknown>
$taroParams?: Record<string, unknown>
$taroPath: string
__data__: any,
data: any
selectComponent: (selector: string) => any
Expand Down

0 comments on commit 7a784ec

Please sign in to comment.