rc1 router “No provider for Router”

瘦欲@ 提交于 2019-12-10 18:28:53

问题


This is a follow up post for an error that come after @Shuhei helped me resolved the config.js problem angular 2 rc router angular2-polyfills.js:349 Error

This is my app.component.ts

import { Component,provide }       from '@angular/core';
import { RouteConfig, ROUTER_DIRECTIVES,Router } from '@angular/router-deprecated';

import { HeroShellComponent } from '../apps/hero/hero-shell.component';
import {HomeComponent} from '../home/home.component';

import {Login} from '../login/auth.component';
import {Authentication} from '../login/auth.service';
import {isLoggedin}  from '../login/auth.isLoggedin';

@Component({
  selector: 'my-app', 
  templateUrl: 'app/appshell/app.component.html',
  styleUrls: ['app/appshell/app.component.css'],
  directives: [ROUTER_DIRECTIVES],
})

@RouteConfig([
    {
        path: '/app/apps/hero/...',
        name: 'Hero Sample',
        component: HeroShellComponent,
    },
    { path: '/app/apps/testapp1', name: 'Testapp1', component: HomeComponent },
    { path: '/', redirectTo: ['Login'] },
    { path: '/home', name: 'Home', component: HomeComponent },
    { path: '/login', name: 'Login', component: Login, useAsDefault: true },   
])
export class AppComponent {

  title = 'Apps';

  constructor(public _auth: Authentication, 
              public router: Router
              ) {}  

  onLogout() {

    this._auth.logout();
    this.router.navigate(['/Login']);
  }

}

All other components injects Router through constructor like this above. Router is certainly provided.

path: '/app/apps/hero/...'

is for the child router redirect.

The error I got is

browser_adapter.ts:78 EXCEPTION: Error: 
Uncaught (in promise): EXCEPTION: Error in :0:0 
ORIGINAL EXCEPTION: No provider for Router!. 

ORIGINAL STACKTRACE: Error: DI Exception 
at NoProviderError.BaseException [as constructor] (npmcdn.com/@angular/core@2.0.0-rc.1/src/facade/…) 
at NoProviderError.AbstractProviderError [as constructor]

If the same code worked in beta, what could make it not working in RC1 router-deprecated?

Update: index.html

<html>
  <head>
    <base href="/">

    <title>My Tests</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">    
    <link rel="stylesheet" href="/app/public/css/styles.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-sham.min.js"></script>           
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.25/system-polyfills.js"></script>      
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>

    <script src="https://code.angularjs.org/2.0.0-beta.17/angular2-polyfills.js"></script>
    <script src="https://code.angularjs.org/tools/system.js"></script>
    <script src="https://code.angularjs.org/tools/typescript.js"></script>
    <script src="/app/systemjs.config.js"></script>

    <script>
      System.import('app/main').then(null, console.error.bind(console));
    </script>
  </head>
  <body>
    <my-app>Loading...</my-app>
    <script>document.write('<base href="' + document.location + '" />');</script>
  </body>
</html>

回答1:


Import ROUTER_DIRECTIVES, Router and Routes from '@angular/router'
Replace @RouteConfig with @Routes

This helped me to get it working. https://angular.io/docs/ts/latest/guide/router-deprecated.html




回答2:


You need to provide the router, something like this :

...
import { RouteConfig, ROUTER_DIRECTIVES, Router, ROUTER_PROVIDERS } from '@angular/router-deprecated';

@Component({
  selector: 'my-app', 
  templateUrl: 'app/appshell/app.component.html',
  styleUrls: ['app/appshell/app.component.css'],
  directives: [ROUTER_DIRECTIVES],
  providers: [ROUTER_PROVIDERS]
})
 ...

More about how providers works here : https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html




回答3:


make sure you have in your index.html:

<script>document.write('<base href="' + document.location + '" />');</script>



回答4:


You should change you code this way if you want to use new Router:

import {Routes} from '@angular/router'

@Component({
  selector: 'my-app', 
  templateUrl: 'app/appshell/app.component.html',
  styleUrls: ['app/appshell/app.component.css'],
  directives: [ROUTER_DIRECTIVES],
})

@Routes([
    {
        path: '/app/apps/hero/...',
        component: HeroShellComponent,
    },
    { path: '/app/apps/testapp1', component: HomeComponent },
    { path: '/', redirectTo: ['Login'] },
    { path: '/home', component: HomeComponent },
    { path: '/login',  component: Login},   
])
export class AppComponent {}

See more doc here https://angular.io/docs/ts/latest/guide/router.html




回答5:


For angular router 3.0.0-beta.1:

I got the similar error, I found out that WebStorm auto import @angular/router-deprecated for me. From the tutorial, it should be @angular/router.

app.component.ts:

import {ROUTER_DIRECTIVES} from "@angular/router";


来源:https://stackoverflow.com/questions/37088964/rc1-router-no-provider-for-router

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