简体   繁体   English

Angular2路由器:更改路由不会首次加载组件

[英]Angular2 router : Changing route doesn't load the component for first time

I am changing the route of angular2 application from '/' to '/xyz'. 我正在将angular2应用程序的路由从“ /”更改为“ / xyz”。 For 'xyz' I have made a component named XYZComponent. 对于“ xyz”,我制作了一个名为XYZComponent的组件。 This XYZComponent implements a hook for router lifecycle named "CanActivate". 此XYZComponent为路由器生命周期实现了一个名为“ CanActivate”的挂钩。 In this hook I am changing the title of the page for new route. 在此挂钩中,我将更改新路线的页面标题。

@CanActivate((next, prev) => { 
   document.title = "mysite | xyz" 
   return true; 
})

Then I implement OnInit for this component and call the method "ngOnInit()". 然后,我为此组件实现OnInit并调用方法“ ngOnInit()”。 In this method I am calling the server to give me some data. 在这种方法中,我呼叫服务器给我一些数据。 This data is stored in a variable named xyzData, which is binded to the template. 此数据存储在名为xyzData的变量中,该变量绑定到模板。 My template is: 我的模板是:

<div class="xyzComp">
 <p>{{xyzData.msg}}</p>
</div>

OnInit is: OnInit是:

ngOnInit(){
    console.log("xyz component init");
    this._myRestfulService.getXyz()
        .subscribe(data => {
            console.log("success",data);
            this.xyzData = data;
        }, err => {
            this.xyzData = {msg:"cannot get data"};
            console.log("error", err);
        });
}

Changing the route is done by calling _route.navigate(["XYZ"]) from the click of a button named xyzBtn, which is present in the header of my app. 通过单击名为xyzBtn的按钮(位于我的应用程序的标题中),单击_route.navigate([“ XYZ”])即可更改路线。 The header is constant for all the routes. 标头对于所有路由都是恒定的。

Problem is that when I click on xyzBtn for first time it calls only whatever written in @CanActivate decorator of XYZComponent. 问题是,当我第一次单击xyzBtn时,它仅调用XYZComponent的@CanActivate装饰器中编写的内容。 Next time when I click xyzBtn,@CanActivate is not called but this time ngOnInit() is called. 下次单击xyzBtn时,不会调用@CanActivate,但是这次将调用ngOnInit()。

Please help me on this. 请帮我。 This problem occurs only when I deploy my code on server. 仅当我在服务器上部署代码时,才会出现此问题。 The deployment server is NGINX server. 部署服务器是NGINX服务器。 On nginx server we have configured to ignore frontend routes. 在nginx服务器上,我们已配置为忽略前端路由。

However my code works perfectly well on development server which is a npm module named lite-server. 但是我的代码在开发服务器上运行得很好,该服务器是一个名为lite-server的npm模块。

Update - It works well on other npm based servers like http-server. 更新-在其他基于npm的服务器(例如http-server)上运行良好。

Update2 - Plunker : https://plnkr.co/edit/E4HNXEjaIP7th77UE4dK?p=preview Update2-Plunker: https ://plnkr.co/edit/E4HNXEjaIP7th77UE4dK ? p = preview

Update3 - I am using a bundle file in server. Update3-我在服务器中使用捆绑文件。 Building the bundle through systemjs builder. 通过systemjs构建器构建捆绑包。 Could that be an issue? 这可能是个问题吗?

https://github.com/angular/angular/issues/8166 https://github.com/angular/angular/issues/8166

The issue is resolved by removing the system-polyfill 通过删除system-polyfill解决了该问题

There is no specific answer to this problem. 没有针对此问题的具体答案。 Its just that both Angular2 and SystemJs are in beta. 只是Angular2和SystemJ都处于beta状态。 You cannot use minified files of angular2 or its dependent modules. 您不能使用angular2或其相关模块的缩小文件。 Also you cannot combine your system js modules with non systemJs javascript files. 同样,您不能将您的系统js模块与非systemJs javascript文件结合使用。

So if you have say - nonsys.js, sys1.js and sys2.js files in your project. 因此,如果您说了-项目中的nonsys.js,sys1.js和sys2.js文件。 You cannot bundle all three of them and serve them from server. 您不能将所有三个捆绑在一起并从服务器提供它们。 But you can combine sys1.js and sys2.js to become sys-all.js and then serve 2 different files from your server to browser. 但是,您可以将sys1.js和sys2.js合并为sys-all.js,然后从服务器向浏览器提供2个不同的文件。

Sorry guys, it isn't just Angular2 problem, but its system js problem. 抱歉,这不仅是Angular2问题,而且是系统js问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM