2

I'm trying to use Angular Routes in Angular 4 app, but the app is not able to load the component matching the requested route:

Here is app-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';

const routes: Routes = [
  { path: 'dashboard', component: DashboardComponent },
  { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
];

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

app.module.ts looks like this:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  declarations: [
    AppComponent,
    DashboardComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
  ],
  providers: [ ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

app.component.html:

<h1>
    Welcome to {{title}}!
</h1>
<router-outlet></router-outlet>

My dashboard.component.html :

<p>dashboard works!</p>

I'm trying to figure out whats wrong, also looked at the Angular 4 Routing tutorial. Please help.

4
  • 1
    import { AppRoutingModule } from './/app-routing.module'; Loot at ".//app.." why two slash? Commented Nov 20, 2017 at 11:42
  • This import was added by ng generate in app.module.ts. However, it tried after removing one slash, didn't work. The component 'AppComponent' doesn't load. Commented Nov 20, 2017 at 11:45
  • 1
    What is the error you are getting ? Commented Nov 20, 2017 at 11:54
  • I have looked at browser console, it says: Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document. at new PathLocationStrategy (common.js:647) at provideLocationStrategy (router.js:6995) at _callFactory (core.js:10651) at _createProviderInstance$1 (core.js:10599) at initNgModule (core.js:10549) at new NgModuleRef_ (core.js:11792) at createNgModuleRef (core.js:11782) at Object.debugCreateNgModuleRef [as createNgModuleRef] (core.js:14092) at NgModuleFactory_.create (core.js:15216) at eval (core.js:5370) Commented Nov 20, 2017 at 12:02

2 Answers 2

5

I found the solution after some troubleshooting, and found the answer on this link. Basically, in app.module.ts, we just have to add this import statement for importing APP_BASE_HREF token:

import { APP_BASE_HREF } from '@angular/common';

And add the APP_BASE_HREF to providers array:

providers: [ {provide: APP_BASE_HREF, useValue: '/' }],

Another solution according to the Angular documentation is to simply add <base href="/"> as the first child of <head> tag.

Sign up to request clarification or add additional context in comments.

Comments

0

well, i got the wrong thing

exports: [RouterModule] 

you are exporting a core module from @angular/router so your AppModule get extended by RouterModule, not your route and instead of creating the whole module just export the const like

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

and import routing in your appmodule

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.