我最近更新了我的angular2版本并遇到了以下问题:
路由器lib不再存在,已被路由器弃用.
我有这个菜单组件:
import { Component } from '@angular/core';
import { DashboardComponent } from './dashboard.component'
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';
@Component({
selector: 'main-menu',
templateUrl: 'app/views/menu.html',
directives: [ROUTER_DIRECTIVES],
providers: [
ROUTER_PROVIDERS
]
})
@RouteConfig([
{
path: '/dashboard',
name: 'Dashboard',
component: DashboardComponent,
useAsDefault: true
}
])
export class MenuComponent {}
Run Code Online (Sandbox Code Playgroud)当我尝试加载它时,应用程序失败,因为它无法解析路由器所需的文件 - 已弃用.我收到以下错误:
关于为什么这不起作用的任何建议,和/或如何更好地完成这项工作,我们非常感谢.
情境概要: 我有一个基于NavComponent选择创建的SelectionComponent.当用户进行选择并创建Selectioncomponent时,有一个导出类实现ngOnInit,以根据所选组件的索引从服务中获取数据(这很好).我还想用其他服务中的其他数据填充SelectionComponent的html模板,这些数据取决于用户的选择.但是,第二个服务依赖于SelectionComponent的导出类中的局部变量,以便从数据库中过滤正确的元数据.该局部变量既不是由作用域定义的,也不是当它试图引用它时,那就是打破应用程序的错误我想?
我尝试过:
- 我也尝试在ngOnInit中添加getLocation调用,但此时局部
变量未定义. - 我尝试使用ngAfterViewInit在ngOnInit之后调用getLocation函数,但局部变量仍未定义.- 我没有像http://learnangular2.com/viewChild/那样添加@ViewChild .
导致我的问题的代码:
import {Component, Input, OnInit, AfterViewInit} from '@angular/core';
import {ActivatedRoute, Params} from '@angular/router';
import {Location} from '@angular/common';
import 'rxjs/add/operator/switchMap';
import {SelectionService}...;
import {Selection}...;
import {LocationService}...;
import {Location}...;
@Component({
selector: 'selection',
templateUrl: 'selection.component.html',
styleUrls: ['selection.component.css']
})
export class SelectionComponent implements OnInit, AfterViewInit {
selection: Selection;
locations: Locations[] = [];
constructor(private selectionService: SelectionService,
private locationService: LocationService,
private route: ActivatedRoute) {}
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.seletionService.getSelectionByName(params['name']))
.subscribe(selection=> …
Run Code Online (Sandbox Code Playgroud)