这是我的app.component.ts
:
@RouteConfig([
{path: '/', name: 'Home', component: HomePageComponent},
{path: '/users/...', name: 'Users', component: UsersComponent},
{path: '/posts', name: 'Posts', component: PostsComponent},
{path: '/*others', redirectTo: ['Home']}
])
@Component({
selector: 'my-app',
templateUrl: 'app/app.template.html',
styleUrls: ['app/app.style.css'],
directives: [ROUTER_DIRECTIVES, PostsComponent, UsersComponent, HomePageComponent, AddUserComponent]
})
export class AppComponent {
constructor(private router:Router) {
}
public isRouteActive(route) {
return this.router.isRouteActive(this.router.generate(route))
}
}
这里是users.component.ts
:
@RouteConfig([
{path: '/new', name: 'AddUser', component: AddUserComponent},
{path: '/', name: 'FakePage', component: FakeComponent, useAsDefault: true},
])
@Component({
selector: 'users',
templateUrl: 'app/users.template.html',
providers: [HTTP_PROVIDERS, ROUTER_PROVIDERS],
directives: [ROUTER_DIRECTIVES],
styles: [`.glyphicon-remove{color: #b20000;} a{text-decoration: none!important;}.btn-add-user{background: #2c3e52;color: white;margin-bottom: 10px}`]
})
export class UsersComponent {
private _users:User;
constructor(private _http:Http) {
var headers = new Headers({"Access-Control-Allow-Methods": "GET,POST"});
var options = new RequestOptions({
headers: headers
});
var users = this._http.get('http://jsonplaceholder.typicode.com/users', options)
.map(res => res.json()).subscribe(data=> {
this._users = data
});
}
}
而且users.template.html
是这样的:
<h1>Users</h1>
<a class="btn btn-add-user" [routerLink]="['AddUser']">Add User</a>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of _users">
<td>{{user.name}}</td>
<td>{{user.email}}</td>
<td><a href="#" class="glyphicon glyphicon-edit"></a></td>
<td><a href="#" class="glyphicon glyphicon-remove"></a></td>
</tr>
</tbody>
</table>
现在,当我访问Users
页面chrome控制台时会引发错误:
错误:组件“ AppComponent”没有名为“ AddUser”的路由。在RouteRegistry.exports.RouteRegistry.RouteRegistry.generate(不推荐使用路由器)上的新BaseException(router-deprecated.umd.js:648)上通过RouteRegistry.exports.RouteRegistry.RouteRegistry._generate(router-deprecated.umd.js:2389) umd.js:2327)位于RootRouter.exports.Router.Router.generate(router-deprecated.umd.js:2984)位于RouterLink.exports.RouterLink.RouterLink._updateLink(router-deprecated.umd.js:3359)位于RouterLink .set [作为routeParams](router-preprecated.umd.js:3371)在DebugAppView._View_UsersComponent0.detectChangesInternal(UsersComponent.template.js:146)在DebugAppView.AppView.detectChanges(core.umd.js:9996)在DebugAppView中。 DebugAppView.AppView.detectViewChildrenChanges(core.umd.js:10016)的detectChanges(core.umd.js:10084)
而且Add User
按钮不起作用。
不要提供ROUTER_PROVIDERS
所有组件。它们只能在根组件中或在以下位置中提供一次bootstrap()
去掉
ROUTER_PROVIDERS
从providers: []
您的UserComponent
并将其添加到AppComponent
暗示
CORS标头
var headers = new Headers({"Access-Control-Allow-Methods": "GET,POST"});
必须由服务器添加以允许CORS请求。在客户端上添加它们无效。
另请参见Access-Control-Allow-Origin不允许使用Origin
无需directives: [...]
添加由路由器添加的组件。仅列出直接在模板中使用的指令(除非它们已经由PLATFORM_DIRECTIVES
like提供ngFor
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句