rc1路由器"没有路由器的提供商"

时间:2016-05-07 13:10:25

标签: angular angular2-routing

这是@Shuhei帮助我解决config.js问题后出现错误的后续帖子angular 2 rc router angular2-polyfills.js:349 Error

这是我的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']);
  }

}

所有其他组件通过上面的构造函数注入Router。 确实提供了路由器

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

用于子路由器重定向。

我得到的错误是

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]

如果相同的代码在测试版中工作,那么什么可能使它在RC1路由器中不起作用?

更新: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>

5 个答案:

答案 0 :(得分:5)

从'@ angular / router'导入ROUTER_DIRECTIVES,路由器和路由 将@RouteConfig替换为@Routes

这帮助我实现了它。 https://angular.io/docs/ts/latest/guide/router-deprecated.html

答案 1 :(得分:3)

您需要提供路由器,如下所示:

...
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]
})
 ...

有关提供商如何在此工作的详情:https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html

答案 2 :(得分:0)

确保您拥有index.html:

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

答案 3 :(得分:0)

如果您想使用新的路由器,您应该以这种方式更改代码:

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 {}

在此处查看更多文档https://angular.io/docs/ts/latest/guide/router.html

答案 4 :(得分:0)

对于角度路由器#include <iostream> #include <vector> struct A { std::vector<int> v = { 1, 2, 3 }; // default population }; struct B { std::vector<int> v; B() : v(4) {} }; int main() { A a; B b; std::cout << a.v.size() << ", " << b.v.size() << "\n"; std::cout << "\n"; for (int v : a.v) { std::cout << v << "\n"; } std::cout << "\n"; for (int v : b.v) { std::cout << v << "\n"; } }

我遇到了类似的错误,我发现WebStorm会自动导入3.0.0-beta.1。从tutorial开始,它应为@angular/router-deprecated

app.component.ts:

@angular/router