Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信小程序第一次进入分包页面出现怪异 #4505

Closed
XanderXueMingJi opened this issue Sep 22, 2019 · 3 comments
Closed

微信小程序第一次进入分包页面出现怪异 #4505

XanderXueMingJi opened this issue Sep 22, 2019 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@XanderXueMingJi
Copy link

XanderXueMingJi commented Sep 22, 2019

问题描述
微信小程序第一次进入分包页面cat, 所有分包页面(例如dog)render内的代码会执行一遍,然后再执行cat内render内的代码 而之后没有这种怪异的情况发生

复现步骤
1,src/目录下新建subpackages/目录,新建页面
2,app.js中声明分包为独立分包
3,在分包内设置cat与dog页面
4,微信开发者工具中新建编译模式,然后在首页点击按钮进入cat页面
5,查看console, 发现cat与dog内的console.log都会执行

[代码只贴截图,不贴文字会被视为无效issue]
[代码只贴截图,不贴文字会被视为无效issue]
[代码只贴截图,不贴文字会被视为无效issue]
image
image
image

```ruby
  config = {
    pages: [
      'pages/index/index'
    ],
    subPackages: [
      {
        root: "packageA",
        pages: [
          "pages/cat/index",
          "pages/dog",
          // "pages/dog"
        ]
      }
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black'
    }
  }

// 提供完整可复现的代码和整理好代码格式,有助于我们快速定位问题,节省你我时间。
// 代码提供不全或代码格式混乱的 issues 【有可能被忽略】,烦请各位注意。


**期望行为**
微信小程序进入分包页面, 其他分包页面不会执行render内的代码

**报错信息**

无

**系统信息**


操作系统: window
Taro 版本: Taro v1.3.12
Node.js 版本 : v10.16.0
平台: 微信开发者工具

**补充信息**
无
@taro-bot
Copy link

taro-bot bot commented Sep 22, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@XanderXueMingJi
Copy link
Author

代码:
subPackagesForCode.zip

@Chen-jj
Copy link
Contributor

Chen-jj commented Oct 11, 2019

@XanderXueMingJi

微信的分包机制导致,第一次跳转到分包页面,会把分包里所有的页面构造一次。即把 page.js 跑一遍。

可以把 dist/packageA/pages/cat/cat.js 和 dist/packageB/pages/dog.js 改成这样测试,

function Init () {
  console.log('Hello, 这是小猫页面')
  return {}
}

Component(Init())
function Init () {
  console.log('Hello, 这是小狗页面')
  return {}
}

Component(Init())

进入 cat 页,可以看到也是跑了两次 log,证明分包内所有页面都被同时构造。

而又因为 render 函数会在页面或组件构造时被提前调用,所以你会看到 dog 页面 render 函数的 log。

无论如何 dog 页面的 render 是会跑两遍的,只不过分包时更提前而已。而 render 又是纯函数,多次调用也不会有问题。

所以推荐保持 render 函数为纯函数,副作用写在其它生命周期中,这样就不会有问题。

@Chen-jj Chen-jj closed this as completed Oct 11, 2019
@Chen-jj Chen-jj added answered question Further information is requested labels Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants