Skip to content

Commit

Permalink
feat(taro-weapp): 增加 this.$componentType 来判断当前 Taro.Component 是页面还是组件 f…
Browse files Browse the repository at this point in the history
…ix #1166
  • Loading branch information
Chen-jj committed Nov 22, 2018
1 parent 777b528 commit b35d1bd
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/best-practice.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ if (process.env.NODE_ENV === 'development') {
}
```

### 使用 `this.$componentType` 来判断当前 Taro.Component 是页面还是组件

`this.$componentType` 可能取值分别为 `PAGE``COMPONENT`,开发者可以根据此变量的取值分别采取不同逻辑。

### 预加载

**微信小程序中**,从调用 `Taro.navigateTo``Taro.redirectTo``Taro.switchTab` 后,到页面触发 componentWillMount 会有一定延时。因此一些网络请求可以提前到发起跳转前一刻去请求。
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-alipay/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class BaseComponent {
_disable = true
_pendingStates = []
_pendingCallbacks = []
$componentType = ''
$router = {
params: {}
}

constructor () {
constructor (props = {}, isPage) {
this.state = {}
this.props = {}
this.$componentType = isPage ? 'PAGE' : 'COMPONENT'
}
_constructor (props) {
this.props = props || {}
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-alipay/src/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function createComponent (ComponentClass, isPage) {
Object.assign(weappComponentConf, {
onLoad (options = {}) {
hasPageInited = false
this.$component = new ComponentClass()
this.$component = new ComponentClass({}, isPage)
this.$component._init(this)
this.$component.render = this.$component._createData
this.$component.__propTypes = ComponentClass.propTypes
Expand Down Expand Up @@ -313,7 +313,7 @@ function createComponent (ComponentClass, isPage) {
} else {
Object.assign(weappComponentConf, {
didMount () {
this.$component = new ComponentClass()
this.$component = new ComponentClass({}, isPage)
this.$component._init(this)
this.$component.render = this.$component._createData
this.$component.__propTypes = ComponentClass.propTypes
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-swan/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class BaseComponent {
_disable = true
_pendingStates = []
_pendingCallbacks = []
$componentType = ''
$router = {
params: {}
}

constructor () {
constructor (props = {}, isPage) {
this.state = {}
this.props = {}
this.$componentType = isPage ? 'PAGE' : 'COMPONENT'
}
_constructor (props) {
this.props = props || {}
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-swan/src/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function createComponent (ComponentClass, isPage) {
data: initData,
created (options = {}) {
isPage && (hasPageInited = false)
this.$component = new ComponentClass()
this.$component = new ComponentClass({}, isPage)
this.$component._init(this)
this.$component.render = this.$component._createData
this.$component.__propTypes = ComponentClass.propTypes
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-weapp/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ class BaseComponent {
_isForceUpdate = false
_pendingStates = []
_pendingCallbacks = []
$componentType = ''
$router = {
params: {}
}

constructor (props = {}) {
constructor (props = {}, isPage) {
this.state = {}
this.props = props
this.$componentType = isPage ? 'PAGE' : 'COMPONENT'
}
_constructor (props) {
this.props = props || {}
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-weapp/src/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ function createComponent (ComponentClass, isPage) {
if (isPage && cacheDataHas(preloadInitedComponent)) {
this.$component = cacheDataGet(preloadInitedComponent, true)
} else {
this.$component = new ComponentClass()
this.$component = new ComponentClass({}, isPage)
}
this.$component._init(this)
this.$component.render = this.$component._createData
Expand Down
2 changes: 2 additions & 0 deletions packages/taro/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ declare namespace Taro {

options?: ComponentOptions;

$componentType: 'PAGE' | 'COMPONENT'

$router: {
params: any
}
Expand Down

0 comments on commit b35d1bd

Please sign in to comment.