Cannot match any routes

后端 未结 5 639
陌清茗
陌清茗 2021-01-07 17:52

My component shows up but i get an error message when the page is loaded. I can\'t seem to solve the error message at all after looking at a bunch of resources.

Erro

相关标签:
5条回答
  • 2021-01-07 18:23

    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}
    ])
    
    0 讨论(0)
  • 2021-01-07 18:27
    • This might be because your server needs to be configured for HTML5 pushState like explained in Angular 2.0 router not working on reloading the browser

    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

    • If you don't have a route for / you might need to make the app navigate to an initial route imperatively like explained in Angular2 router (@angular/router), how to set default route?

    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.

    0 讨论(0)
  • 2021-01-07 18:31

    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']);
    
    0 讨论(0)
  • 2021-01-07 18:31

    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,

    0 讨论(0)
  • 2021-01-07 18:41

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

    { 
        path: '', 
        component: HomeComponent 
    }
    
    0 讨论(0)
提交回复
热议问题