编辑这里是我的柱塞:Plnkr.co/edit/qMBc5j62jQoS BJLsFYgr4?P=预览
我在用TypeScript。
我得到了以下错误:
在路由器实例化过程中出错!(RouterLink ->路由器)。和(RouterOutlet ->路由器)
在这个论坛上,我尝试过不同的解决方案,包括在index.html中添加/更改index.html或在引导中添加APP_BASE_HREF。
我一直在使用http://plnkr.co/edit/d8pewJf9kVqD3kTSvQmK?p=preview的官方角度柱塞教程的代码,这是很好的工作。
我的app.component.ts看起来如下:
import { Component } from 'angular2/core';
import {LandingComponent} from './landing.component';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';
@Component({
selector: 'my-app',
template: `
<h1>Hello</h1>
<h1>{{title}}</h1>
<nav>
<a [routerLink]="['Landing']">Heroes</a>
</nav>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES],
providers: [ROUTER_PROVIDERS]
})
@RouteConfig({
{
path: '/landing',
name: 'Landing',
component: LandingComponent,
useAsDefault: true
}
})
export class AppComponent {
title = 'Win Free!';
}main.ts
import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';
bootstrap(AppComponent);index.html
<html>
<head>
<script>document.write('<base href="' + document.location + '" />'); </script>
<title>Angular 2 QuickStart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script>
<script src="https://npmcdn.com/angular2@2.0.0-beta.12/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.12/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://npmcdn.com/typescript@1.8.9/lib/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.12/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.12/angular2.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.12/router.dev.js"></script>
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
packages: {'app': {defaultExtension: 'ts'}}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>谢谢。
发布于 2016-03-28 12:47:55
从providers: [ROUTER_PROVIDERS]中删除AppComponent。ROUTER_PROVIDERS应该由整个应用程序共享,只需将它添加到boostrap(AppComponent, [ROUTER_PROVIDERS])中。
如果您也将它们添加到组件providers上,那么组件将获得一个新实例,这将阻止它们按预期工作。
更新
在您的柱塞(Edit ...之后的链接)中,ROUTER_PROVIDERS只在组件上有,而在bootstrap()中没有,但是应该是相反的。@RouteConfig()中还有一个语法问题,它需要一个数组,但是是{} (而不是[])。
工作柱塞
https://stackoverflow.com/questions/36261010
复制相似问题