[英]Angular redirects to wrong url?
I'm learning angular and I would like to make a simple app, where the user can jump between two components back and forth.我正在学习 angular,我想制作一个简单的应用程序,用户可以在其中来回切换两个组件。 One way it is working.
它的一种工作方式。 But when I try like this to go back from navbar to login:
但是当我尝试这样从导航栏返回登录时:
<a routerLink="login" class="nav-item nav-link" href="#">Logout</a>
I get an error that it tries to reach "navbar/login" (instead of /login) which obviously does not exist.我收到一个错误,提示它试图到达显然不存在的“导航栏/登录”(而不是/登录)。
When I try this way:当我尝试这种方式时:
<a (click)="handleSubmitLogout()" class="nav-item nav-link" href="#">Logout</a>
And in ts:在 ts 中:
handleSubmitLogout() {
this._router.navigateByUrl('login');
}
It goes back to login but after a second it gets redirected to empty url.它返回登录,但一秒钟后它被重定向到空 url。
My app-routing:我的应用程序路由:
import {LoginComponent} from './login/login.component';
import {NavbarComponent} from './navbar/navbar.component';
const routes: Routes = [
{path: 'login', component: LoginComponent},
{path: 'navbar', component: NavbarComponent},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Thank you for your help!感谢您的帮助!
<a routerLink="/login" class="nav-item nav-link" >Logout</a>
or或者
handleSubmitLogout() { this._router.navigate(['/login']); }
Assign the anchor tag that you want to add the route to the routerLink attribute.将要添加路由的锚标记分配给 routerLink 属性。
<a routerLink="/login" class="nav-item nav-link" >Logout</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.