我正在学习 Angular2,并遵循“英雄之旅”示例,当我为路由设置详细信息页面时,我从 webpack 得到了这个编译错误:
ERROR in ./ts/router/route-hero-detail.component.ts
(25,23): error TS2339: Property 'switchMap' does not exist on type 'Observable<Params>'.
我正在使用 webpack 来管理包的东西,
下面是JS代码:
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
@Component({
moduleId: module.id,
selector: 'my-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: [ './hero-detail.component.css' ]
})
export class RouteHeroDetailComponent implements OnInit {
hero: Hero;
constructor(
private heroService: HeroService,
private route: ActivatedRoute,
private location: Location
) {}
ngOnInit(): void {
this.route.params.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe((hero: Hero) => this.hero = hero);
}
goBack(): void {
this.location.back();
}
}
包.json:
{
"name": "environment",
"version": "1.0.0",
"description": "I will show you how to set up angular2 development environment",
"keywords": [
"angular2",
"environment"
],
"scripts": {
"start": "webpack-dev-server --hot--host 0.0.0.0"
},
"author": "Howard.Zuo",
"license": "MIT",
"dependencies": {
"@angular/common": "^2.4.5",
"@angular/compiler": "^2.4.5",
"@angular/core": "^2.4.5",
"@angular/forms": "^2.4.5",
"@angular/platform-browser": "^2.4.5",
"@angular/platform-browser-dynamic": "^2.4.5",
"@angular/router": "^3.4.8",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.20",
"@types/node": "^7.0.5",
"bootstrap": "^4.0.0-alpha.6",
"core-js": "^2.4.1",
"rxjs": "5.0.3",
"zone.js": "^0.7.6"
},
"devDependencies": {
"@types/core-js": "^0.9.35",
"ts-loader": "^2.0.0",
"typescript": "^2.1.5",
"webpack": "^2.2.0",
"webpack-dev-server": "^2.2.0"
}
}
webpack.config.js:
const path = require('path');
module.exports = {
entry: {
index: "./ts/index.ts"
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: 'dist/'
},
module: {
exprContextCritical: false,
rules: [
{
test: /\.ts$/,
use: ['ts-loader']
}
]
},
resolve: {
extensions: [
'.js',
'.ts'
]
}
};
英雄服务.ts:
import {Injectable} from '@angular/core';
import {Hero} from './hero';
import {HEROES} from './mock-heroes';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class HeroService {
getHeroes() : Promise<Hero[]>{
return Promise.resolve(HEROES);
}
getHero(id: number): Promise<Hero> {
return this.getHeroes()
.then(heroes => heroes.find(hero => hero.id === id));
}
}
原文由 Sean.huang 发布,翻译遵循 CC BY-SA 4.0 许可协议
此问题已修复,请检查以下内容: