作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是@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']);
}
}
所有其他组件都像上面这样通过构造函数注入(inject)路由器。 当然提供了路由器。
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]
如果相同的代码在 beta 中工作,是什么让它在 RC1 router-deprecated 中不工作?
更新: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>
最佳答案
从“@angular/router”导入 ROUTER_DIRECTIVES、路由器和路由
将@RouteConfig 替换为@Routes
这帮助我让它发挥作用。 https://angular.io/docs/ts/latest/guide/router-deprecated.html
关于angular - rc1 路由器 "No provider for Router",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37088964/
我是一名优秀的程序员,十分优秀!