Skip to content

Commit

Permalink
fix(cli): 修复h5端入口文件的生命周期处理错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed May 7, 2019
1 parent 8ef960a commit caf5269
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions packages/taro-cli/src/h5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ class Compiler {

const initPxTransformNode = toAst(`Taro.initPxTransform(${JSON.stringify(pxTransformConfig)})`)
const additionalConstructorNode = toAst(`Taro._$app = this`)
const callComponentDidShowNode = toAst(`this.componentDidShow()`)
const callComponentDidHideNode = toAst(`this.componentDidHide()`)
const initTabbarApiNode = toAst(`Taro.initTabBarApis(this, Taro)`)

ast = babel.transformFromAst(ast, '', {
plugins: [
Expand Down Expand Up @@ -407,22 +410,19 @@ class Compiler {
}

if (tabBar && isComponentWillMount) {
const initTabBarApisCallNode = toAst(`Taro.initTabBarApis(this, Taro)`)
node.body.body.push(initTabBarApisCallNode)
node.body.body.push(initTabbarApiNode)
}

if (hasConstructor && isConstructor) {
node.body.body.push(additionalConstructorNode)
}

if (hasComponentDidShow && isComponentDidMount) {
const componentDidShowCallNode = toAst(`this.componentDidShow()`)
node.body.body.push(componentDidShowCallNode)
node.body.body.push(callComponentDidShowNode)
}

if (hasComponentDidHide && isComponentWillUnmount) {
const componentDidHideCallNode = toAst(`this.componentDidHide()`)
node.body.body.unshift(componentDidHideCallNode)
node.body.body.unshift(callComponentDidHideNode)
}
}
},
Expand All @@ -432,16 +432,12 @@ class Compiler {
if (hasComponentDidShow && !hasComponentDidMount) {
node.body.push(t.classMethod(
'method', t.identifier('componentDidMount'), [],
t.blockStatement([
toAst('super.componentDidMount && super.componentDidMount()') as t.Statement
]), false, false))
t.blockStatement([callComponentDidShowNode]), false, false))
}
if (hasComponentDidHide && !hasComponentWillUnmount) {
node.body.push(t.classMethod(
'method', t.identifier('componentWillUnmount'), [],
t.blockStatement([
toAst('super.componentWillUnmount && super.componentWillUnmount()') as t.Statement
]), false, false))
t.blockStatement([callComponentDidHideNode]), false, false))
}
if (!hasConstructor) {
node.body.push(t.classMethod(
Expand All @@ -451,10 +447,8 @@ class Compiler {
if (tabBar) {
if (!hasComponentWillMount) {
node.body.push(t.classMethod(
'method', t.identifier('componentWillMount'), [],
t.blockStatement([
toAst('super.componentWillMount && super.componentWillMount()') as t.Statement
]), false, false))
'method', t.identifier('componentWillMount'), [ initTabbarApiNode ],
t.blockStatement([]), false, false))
}
if (!stateNode) {
stateNode = t.classProperty(
Expand Down

0 comments on commit caf5269

Please sign in to comment.