Cannot match any routes

妖精的绣舞 提交于 2019-12-01 02:25:17
Günter Zöchbauer

Also ensure you have a <base href="/"> tag at the beginning of the <head> tag or provided by boostrap(...) as explained in Angular 2 router no base href set

Hint: The @angular/router router also is deprecated as @angular/router-deprecated and a new router will be shipped again :-/ If you are about to switch to @angular/router it's probably better to postpone until the new and hopefully final router is available.

When I have had this problem I typically add

<base href="./"> 

to the <head> <base href="./"> </head> of html file.

Then when importing any files in NG2

import { Auth }                 from './auth.service';

This is the prefered way to do base per the documentation,

if you have child component and you want to navigate use this way

router servive

const routes: Routes = [
  {
    path: '',
    component: LoginComponent
  },
  {
    path: 'home',
    component: HomeComponent,
    children: [
      {
        path: 'enter',
        component: EnterDetailsComponent,
      },
      {
        path: 'find',
        component: FindDetailsComponent,
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

use navigate([]) method this way

component constructor

this is how this._router comes

constructor( private _router: Router) {}

this._router.navigate(['/home/find']);

plz make sure <base href="/"> tag in your head tag and you can use useAsDefault: true attr like this :

@Routes([
    {path: '/login', component: LoginComponent,useAsDefault: true}
])
Sancho George

just add a route for '' the exception will go.

{ 
    path: '', 
    component: HomeComponent 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!