Skip to content

Commit

Permalink
feat(taro): 支持app及page的onShow/onHide方法
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Apr 23, 2018
1 parent 8ca7b82 commit 04e3ae9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
17 changes: 11 additions & 6 deletions packages/taro/src/component.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isEmptyObject } from './util'
import { enqueueRender } from './render-queue'
import { updateComponent } from './lifecycle'

Expand Down Expand Up @@ -39,16 +40,20 @@ class Component {
}
}

Object.getOwnPropertyNames(this.$$components).forEach(name => {
this.$$components[name]._initData(this.$root, this)
})
if (this.$$components && !isEmptyObject(this.$$components)) {
Object.getOwnPropertyNames(this.$$components).forEach(name => {
this.$$components[name]._initData(this.$root, this)
})
}
}
_init (scope) {
this.$scope = scope
this.$app = getApp()
Object.getOwnPropertyNames(this.$$components).forEach(name => {
this.$$components[name]._init(this.$scope)
})
if (this.$$components && !isEmptyObject(this.$$components)) {
Object.getOwnPropertyNames(this.$$components).forEach(name => {
this.$$components[name]._init(this.$scope)
})
}
}
// rewrite when compile
_createData () {
Expand Down
22 changes: 21 additions & 1 deletion packages/taro/src/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@ function createApp (AppClass) {
const app = new AppClass()
const weappAppConf = {
onLaunch (options) {
app._init(this)
app.$router = {
params: options
}
if (app.componentWillMount) {
app.componentWillMount()
}
if (app.componentDidMount) {
app.componentDidMount(options)
app.componentDidMount()
}
},

onShow (options) {
Object.assign(app.$router.params, options)
if (app.componentDidShow) {
app.componentDidShow()
}
},

onHide () {
if (app.componentDidHide) {
app.componentDidHide()
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/taro/src/create-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ function createPage (PageClass) {
onReady () {
componentTrigger(page, 'componentDidMount')
},
onShow () {
componentTrigger(page, 'componentDidShow')
},
onHide () {
componentTrigger(page, 'componentDidHide')
},
onUnload () {
componentTrigger(page, 'componentWillUnmount')
},
Expand Down

0 comments on commit 04e3ae9

Please sign in to comment.