简体   繁体   中英

No provider for RouterOutletMap! in angular2 app

I am trying to make a routing for my angular2 app. I have seen many questions like this and this but none of them helped me.

Here is my app.route.ts :

import { provideRouter, RouterConfig } from '@angular/router';
import {FormComponent} from './form.component';
import {AboutComponent} from './about-this.component';

const routes: RouterConfig = [
    { path:'home' , component: FormComponent  },
    { path:'about', component: AboutComponent },
    { path:'**'   , component: FormComponent  }
];

export const appRouterProviders = [
  provideRouter(routes)
]

and then here is my root component:

import { ROUTER_DIRECTIVES, Router } from '@angular/router';
import { Component } from '@angular/core';
import {NavbarComponent} from './shared/navbar.component';
import {appRouterProviders} from './app.routes'


@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  directives:[NavbarComponent,ROUTER_DIRECTIVES],
  providers: [appRouterProviders]
})

export class AppComponent {
     title = "Here is Root!";
  }

and in the temple is( app.component.html ):

<router-outlet></router-outlet>

As you can see, I have used ./app.routes as my route provider. As a result, I expect, when i use the http://localhost:4200/home it brings the form component, but it does not happen. Also, there is no error in console. What is going wrong?

Update: I also, bootstraped my app with main.ts :

import { bootstrap }  from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
import {appRouterProviders } from './app.routes';

bootstrap(AppComponent,[appRouterProviders] );

需要在bootstrap提供路由器

bootstrap(AppComponent, [appRouterProviders])

Based off of the comments in the above answers I have a few solutions you could try...

First: In the package.json file change

"@angular/router": "whatever version you have"

to this

"@angular/router": "3.0.0-beta.2"

Also if you Package.json file doesn't look something like the quickstart here then I would suggest coping it over to your project, make the change I noted above, and run an npm install on your project. Then attempt to start your app and see what you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM