简体   繁体   English

router.navigateByUrl()导致导航到根并重新加载,角度为2.4.7

[英]router.navigateByUrl() causes navigation to root and reload, angular 2.4.7

navigateByUrl causes navigation to root and reload navigationByUrl导致导航植根并重新加载

When I use router.navigate() or navigateByUrl(), I get a reload - a call to the root. 当我使用router.navigate()或navigationByUrl()时,我会重新加载-调用根目录。 The Chrome Dev tools console shows Navigated to http://localhost/ Chrome开发者工具控制台显示Navigated to http://localhost/

Navigation via links with <a [routerLink]= works correctly. 通过<a [routerLink]=链接进行的导航正常工作。

app 应用程式

  • was generated using angular-cli. 是使用angular-cli生成的。 Components were generated then modified. 生成组件然后进行修改。
  • has 3 components test1, test11 (a child of test1) and test2 有3个组件:test1,test11(test1的子级)和test2
  • navigating between test11 and test2 works fine via anchor tag links with [routerLink] (declarative) 通过带有[routerLink](说明性)的锚标记链接,可以在test11和test2之间导航正常
  • navigating from test2 to test11 by using (click) event and programmatic (imperative) navigation call this.router.navigateByUrl('/test1/test11'); 使用(click)事件从导航到test11并通过程序化(命令式)导航调用this.router.navigateByUrl('/test1/test11');

environment 环境

angular-cli: 1.0.0-beta.28.3 node: 7.5.0 os: win32 x64 @angular/common: 2.4.7 @angular/compiler: 2.4.7 @angular/core: 2.4.7 @angular/forms: 2.4.7 @angular/http: 2.4.7 @angular/platform-browser: 2.4.7 @angular/platform-browser-dynamic: 2.4.7 @angular/router: 3.4.7 @angular/compiler-cli: 2.4.7

code

Is published to GitHub, published to GitHub https://github.com/michaelkariv/angular_router_demo 发布到GitHub, 发布到GitHub https://github.com/michaelkariv/angular_router_demo

served by ng serve using production environment 使用生产环境服务

serve --host localhost --port 80 --live-reload false -prod

What am I doing wrong? 我究竟做错了什么?

As per your code, when anchor clicked it will trigger browser's default navigation 根据您的代码,单击锚点时将触发浏览器的默认导航

<a href="" (click)="goToTest11()"> router.navigateByUrl('../test1/test11')</a>

to fix that use event.preventDefault() 修复使用event.preventDefault()

<a href="" (click)="$event.preventDefault();goToTest11();"> router.navigateByUrl('../test1/test11')</a>

I haven't seen your code so this is just a guess, but I think you have missed router-outlet in your app.component.html 我还没有看到您的代码,所以这只是一个猜测,但是我认为您在app.component.html中错过了路由器插座。

<div>
    <router-outlet></router-outlet>
</div>

Add the above code to your app.component.html file in the end 最后将上述代码添加到您的app.component.html文件中

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

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