简体   繁体   English

用户手动更改导航栏时路由Angular2应用程序?

[英]Route Angular2 application when user changes nav bar manually?

I'm running the Angular2 sample tutorial app , and when I click links that navigate the application, such as "dashboard" takes me to localhost:3000/dashboard and heroes takes me to localhost:3000/heros without the whole app reloading as expected. 我正在运行Angular2示例教程应用程序 ,当我单击导航该应用程序的链接时,例如“仪表板”将我带到localhost:3000/dashboardheroes将我带到localhost:3000/heros而整个应用程序不会按预期方式重新加载。

Now if I am at localhost:3000/dashboard and I manually modify the URL to localhost:3000/heros and hit enter, the app reloads and takes me to the heros view, however if I do this with Gmail and other javascript applications the application switches the view without reloading. 现在,如果我位于localhost:3000/dashboard并且手动将URL修改为localhost:3000/heros并按Enter,应用程序将重新加载并带我进入英雄视图,但是如果我使用Gmail和其他javascript应用程序执行此操作切换视图而无需重新加载。

What's the name of the components that allow this functionality? 允许此功能的组件的名称是什么?

Is there an Angular or Angular2 way to implement this? 有没有Angular或Angular2的方法来实现呢?

This may not be possible with the PathLocationStrategy . 使用PathLocationStrategy可能无法做到这PathLocationStrategy Gmail, Play Music, and other sites I see this functionality on all have a # in their URLs. Gmail,Play音乐和其他网​​站,我看到的所有功能在其网址中都带有#

I switched the application to the HashLocationStrategy and this now functions as such. 我将应用程序切换到HashLocationStrategy ,现在它可以正常运行了。

The switch is facilitated by the adding an import to main.ts and configuring the router to use it. 通过将导入添加到main.ts并配置路由器以使用它,可以方便地进行切换。

import {Location, LocationStrategy, HashLocationStrategy} from '@angular/common';

bootstrap(AppComponent, [ APP_ROUTER_PROVIDERS, {provide: LocationStrategy, useClass: HashLocationStrategy}, HTTP_PROVIDERS, ]);

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

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