Skip to content

Commit

Permalink
feat(weapp|tt|swan|alipay): 小程序端增加 this.$router.path 标识当前页面路径
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 18, 2018
1 parent ace925b commit 1f3c505
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/taro-alipay/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class BaseComponent {
_pendingCallbacks = []
$componentType = ''
$router = {
params: {}
params: {},
path: ''
}

constructor (props = {}, isPage) {
Expand Down
7 changes: 6 additions & 1 deletion packages/taro-alipay/src/create-component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getCurrentPageUrl } from '@tarojs/utils'

import { isEmptyObject } from './util'
import { updateComponent } from './lifecycle'

const anonymousFnNamePreffix = 'funPrivate'
const componentFnReg = /^__fn_/
const pageExtraFns = ['onTitleClick', 'onOptionMenuClick', 'onPageScroll', 'onPullDownRefresh', 'onReachBottom', 'onShareAppMessage']
Expand Down Expand Up @@ -219,7 +222,8 @@ export function componentTrigger (component, key, args) {
component._dirty = true
component._disable = true
component.$router = {
params: {}
params: {},
path: ''
}
component._pendingStates = []
component._pendingCallbacks = []
Expand Down Expand Up @@ -285,6 +289,7 @@ function createComponent (ComponentClass, isPage) {
this.$component.render = this.$component._createData
this.$component.__propTypes = ComponentClass.propTypes
Object.assign(this.$component.$router.params, options)
this.$component.$router.path = getCurrentPageUrl()
initComponent.apply(this, [ComponentClass, isPage])
},

Expand Down
2 changes: 1 addition & 1 deletion packages/taro-router/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ export interface History {
// location: Location;
// push: any;
// replace: any;
}
}
3 changes: 2 additions & 1 deletion packages/taro-swan/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class BaseComponent {
_pendingCallbacks = []
$componentType = ''
$router = {
params: {}
params: {},
path: ''
}

constructor (props = {}, isPage) {
Expand Down
7 changes: 6 additions & 1 deletion packages/taro-swan/src/create-component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getCurrentPageUrl } from '@tarojs/utils'

import { isEmptyObject, noop } from './util'
import { updateComponent } from './lifecycle'

const privatePropValName = 'privateTriggerObserer'
const anonymousFnNamePreffix = 'funPrivate'
const componentFnReg = /^__fn_/
Expand Down Expand Up @@ -185,7 +188,8 @@ export function componentTrigger (component, key, args) {
component._dirty = true
component._disable = true
component.$router = {
params: {}
params: {},
path: ''
}
component._pendingStates = []
component._pendingCallbacks = []
Expand Down Expand Up @@ -250,6 +254,7 @@ function createComponent (ComponentClass, isPage) {
this.$component.__propTypes = ComponentClass.propTypes
Object.assign(this.$component.$router.params, options)
if (isPage) {
this.$component.$router.path = getCurrentPageUrl()
initComponent.apply(this, [ComponentClass, isPage])
}
},
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-tt/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class BaseComponent {
_pendingCallbacks = []
$componentType = ''
$router = {
params: {}
params: {},
path: ''
}

constructor (props = {}, isPage) {
Expand Down
7 changes: 6 additions & 1 deletion packages/taro-tt/src/create-component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getCurrentPageUrl } from '@tarojs/utils'

import { isEmptyObject, noop } from './util'
import { updateComponent } from './lifecycle'

const privatePropValName = 'privateTriggerObserer'
const anonymousFnNamePreffix = 'funPrivate'
const componentFnReg = /^__fn_/
Expand Down Expand Up @@ -188,7 +191,8 @@ export function componentTrigger (component, key, args) {
component._dirty = true
component._disable = true
component.$router = {
params: {}
params: {},
path: ''
}
component._pendingStates = []
component._pendingCallbacks = []
Expand Down Expand Up @@ -253,6 +257,7 @@ function createComponent (ComponentClass, isPage) {
this.$component.__propTypes = ComponentClass.propTypes
Object.assign(this.$component.$router.params, options)
if (isPage) {
this.$component.$router.path = getCurrentPageUrl()
initComponent.apply(this, [ComponentClass, isPage])
}
},
Expand Down
16 changes: 14 additions & 2 deletions packages/taro-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import shallowEqual from './shallow-equal'

function addLeadingSlash (path) {
return path.charAt(0) === '/' ? path : '/' + path
}

function getCurrentPageUrl () {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
return addLeadingSlash(currentPage.route || currentPage.__route__)
}

export default {
shallowEqual
shallowEqual,
getCurrentPageUrl
}

export {
shallowEqual
shallowEqual,
getCurrentPageUrl
}
3 changes: 2 additions & 1 deletion packages/taro-weapp/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class BaseComponent {
_pendingCallbacks = []
$componentType = ''
$router = {
params: {}
params: {},
path: ''
}

constructor (props = {}, isPage) {
Expand Down
7 changes: 6 additions & 1 deletion packages/taro-weapp/src/create-component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { getCurrentPageUrl } from '@tarojs/utils'

import { isEmptyObject, noop } from './util'
import { updateComponent } from './lifecycle'
import { cacheDataSet, cacheDataGet, cacheDataHas } from './data-cache'

const privatePropValName = '__triggerObserer'
const anonymousFnNamePreffix = 'funPrivate'
const componentFnReg = /^__fn_/
Expand Down Expand Up @@ -250,7 +253,8 @@ export function componentTrigger (component, key, args) {
component._dirty = true
component._disable = true
component.$router = {
params: {}
params: {},
path: ''
}
component._pendingStates = []
component._pendingCallbacks = []
Expand Down Expand Up @@ -356,6 +360,7 @@ function createComponent (ComponentClass, isPage) {
weappComponentConf.methods['onLoad'] = function (options = {}) {
if (this.$component.__isReady) return
Object.assign(this.$component.$router.params, options)
this.$component.$router.path = getCurrentPageUrl()
initComponent.apply(this, [ComponentClass, isPage])
}
weappComponentConf.methods['onReady'] = function () {
Expand Down

0 comments on commit 1f3c505

Please sign in to comment.