Html Angular 2[routerLink]进行导航,但不更新url
我的链接Html Angular 2[routerLink]进行导航,但不更新url,html,angular,typescript,routing,Html,Angular,Typescript,Routing,我的链接 <a [routerLink]="['/login']"> <i class="glyphicon glyphicon-user"></i> Login </a> 导航到拍卖、拍卖/:id和供应商/:id的RouterLink按预期工作,但登录和注册不起作用。当我单击其中任何一个链接时,我会导航到正确的组件,但URL会在返回到localhost:xxxx/previousUrl之前的一瞬间更改为local
<a [routerLink]="['/login']">
<i class="glyphicon glyphicon-user"></i> Login
</a>
导航到拍卖、拍卖/:id和供应商/:id的RouterLink按预期工作,但登录和注册不起作用。当我单击其中任何一个链接时,我会导航到正确的组件,但URL会在返回到localhost:xxxx/previousUrl之前的一瞬间更改为localhost:xxxx/login
这是另一个routerlink,它按预期工作
<a [routerLink]="['/auction', auction.id]">
{{category.name }} - {{auction.name}}
</a>
请尝试以下方式登录和注册:
<a routerLink="/login">
<i class="glyphicon glyphicon-user"></i> Login
</a>
当您有子路由时,最好使用子路由:[]。 这是您使用它的方式:
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: UserRegisterComponent },
{ path: 'auctions', component: AuctionListComponent, children: [
{ path: ':id', canActivate: [ AuctionDetailGuard ], component: AuctionDetailComponent },
{ path: ':id/edit', component: AuctionDetailEditComponent }
]},
{ path: 'supplier/:id', component: SupplierDetailComponent }
];
// This is what you can place on the item
<a [routerLink]="[idOfItem]">Link to Item</a>
// And this method you call in your component.ts file
// Add relativeTo to make it relative to the current route
// so you'll add the "edit" after the called ID.
this.router.navigate(['edit'], { relativeTo: this.route });
我得到了完全相同的结果,所以没有任何改变只是一个简写。也就是说,速记应该是首选,因为速记的语法非常糟糕。你的浏览器控制台有错误吗?你有路由器吗。navigateXxx。。。在LoginComponent或UserRegisterComponent中的任何地方?是的,原来是我的登录表单中的错误导致了它
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: UserRegisterComponent },
{ path: 'auctions', component: AuctionListComponent, children: [
{ path: ':id', canActivate: [ AuctionDetailGuard ], component: AuctionDetailComponent },
{ path: ':id/edit', component: AuctionDetailEditComponent }
]},
{ path: 'supplier/:id', component: SupplierDetailComponent }
];
// This is what you can place on the item
<a [routerLink]="[idOfItem]">Link to Item</a>
// And this method you call in your component.ts file
// Add relativeTo to make it relative to the current route
// so you'll add the "edit" after the called ID.
this.router.navigate(['edit'], { relativeTo: this.route });