Angular2路由器和的OnInit [英] Angular2 Router and OnInit

查看:207
本文介绍了Angular2路由器和的OnInit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始有与Angular2路由器困难。问题是,那名路由器电源插座外组件将调用其 ngOnInit 方法。但是,这是在路由器出口内的部件不会。

I recently started having difficulty with the Angular2 router. The issue was that components that were outside the router-outlet would have their ngOnInit methods called. But components that were inside the router-outlet would not.

我喝了<一个href=\"http://stackoverflow.com/questions/36162649/angular2-router-interacting-with-material-design-lite\">$p$pvious与Angular2路由器以及问题。但是,这个问题似乎与外部JavaScript code,而这一次似乎是显然与Angular2生命周期挂钩的问题。

I'd had previous issues with the Angular2 router as well. But that issue seemed to be related to external Javascript code whereas this one seemed to be clearly an issue with the Angular2 lifecycle hooks.

我把一些Plunker的,但不能重现的问题。所以这就是问题所在。事实证明,我想通了什么问题是(而且是在一些其他的东西我读暗示)。所以我张贴这是一个问题,我会包括哪些,我发现在的情况下别人的答案运行到这个问题。

I put together some Plunker's but couldn't reproduce the issues. So that is the problem. It turns out, I figured out what the issue was (and it was hinted at in some other things I'd read). So I'm posting this as a question and I'll include what I found as an answer in case anybody else runs into this issue.

推荐答案

正如我前面提到的,我会看到与Angular2路由器等问题。现在,我已经挖成这个多了,我想他们是相关的。

As I mentioned before, I'd see other issues with the Angular2 router. Now that I've dug into this more, I think they are related.

我在这里造材的走势一点,不使用SystemJS。相反,我只是做了一个简单的WebPack版本。这似乎是那里的问题所在。我见过几个,他们提到的剧本标记的顺序很重要的其他问题和计算器的问题。那么,既然的WebPack 需要这样的东西直接控制离我而去,我并没有真正引起重视。但事实证明,这是非常重要的。

I'm bucking the trend here a little and not using SystemJS. Instead, I'm just doing a simple webpack build. That seems to be where the issue lies. I'd seen a few other issues and stackoverflow questions where they mentioned the importance of the order of script tags. Well, since webpack takes direct control of such things away from me, I didn't really pay attention. But it turns out it is important.

我目前正在从生产的WebPack A bundle.js 文件。但我没有AP preciate的是,你还必须找到一种方法,包括 angular2-polyfills.js 在应用程序中。我有种以为它会以某种方式得到由的WebPack 包括弄好了,但事实并非如此。

I'm currently producing a bundle.js file from webpack. But what I didn't appreciate is that you also have to find a way to include the angular2-polyfills.js in your application. I kind of assumed it would somehow get included by webpack somehow, but it doesn't.

我想的第一件事是简单地把它添加到&LT;头&gt;我的index.html 部分。这会将它的的WebPack 之外,这似乎是足够的。我还发现,我可以只包括通过增加:

The first thing I tried was to simply add it into the <head> section of my index.html. This loads it outside of webpack, and that seems to be sufficient. I also found that I could just include by adding:

require('angular2/bundles/angular2-polyfills');

......在我的WebPack入口点。

...in my webpack entrypoint.

一旦 angular2 -polyfills.js 文件已经到位,生命周期挂钩都拿到正常调用。

Once the angular2-polyfills.js file was in place, the lifecycle hooks were getting properly called.

这篇关于Angular2路由器和的OnInit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆