Angular 2 router navigates undefined

1k views Asked by At

Every time I click on a link and a new component is loaded, the following error is thrown.

enter image description here

The line that zone shows like responsive-height.component.ts:111 is wrong, there's no http connections in there.

In previous versions of angular, this only happens with built files, not in development environment, but up to 2.2, it happens in dev mode as well.

I don't know if the problem is related to angular2-router or angular-cli.

Currently I use angular 2.3.1 with [email protected].

Any idea?

UPDATE: My routes are there, I delete several in the middle that are the same, and change some domain names by foo bar.

const routes: Routes = [
  {
    path: '',
    component: Component1,
    pathMatch: 'full'
  },
  {
    path: 'foo/bar',
    component: Component2
  },
  {
    path: 'user/profile',
    component: ProfileComponent
  },
  {
    path: '404',
    component: NotFoundComponent
  },
  {
    path: '**',
    redirectTo: '404',
    pathMatch: 'full'
  }
];

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

UPDATE 2: The aplicación works properly, no weird behaviors. But this error is thrown and I want to fix it.

UPDATE 3:

The app.module:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ErrorModule,
    HeaderModule,
    HttpModule,
    SharedModule,
    RoutingModule,
    UserModule
  ],
  providers: [
    {
      provide: LOCALE_ID,
      useValue: window.navigator.userLanguage || window.navigator.language
    },
    {provide: RequestOptions, useClass: DefaultRequestOptions},
    {provide: ConnectionBackend, useClass: XHRBackend},
    {provide: Http, useExisting: HttpInterceptor},
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}
3

There are 3 answers

0
Serginho On BEST ANSWER

I found the error, background-image: none in a css file causes that error.

1
Günter Zöchbauer On

For empty path routes without children or redirect, you need pathMatch: 'full':

  {
    path: '',
    component: Component1,
    pathMatch: 'full'
  },

otherwise the router keeps looking for child routes.

2
Adam Michalski On

Missing declarations for components or wrong path in imports

declarations: [
    AppComponent,
    Component1,
    Component2
    ...    
  ]

Check your paths. Provide plunker with your code

app.routes.ts:

const routes: Routes = [
  {
    path: '',
    component: HeaderComponent
  },
  {
    path: 'shared',
    component: SharedComponent
  },
  {
    path: '**',
    redirectTo: '404'
  }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: []
})
export class RoutingModule {
}

shared.module.ts similiar to header.module.ts

@NgModule({
  imports: [
    CommonModule    
  ],
  declarations: [SharedComponent]
})
export class SharedModule { }

Routes without errors.