我正在学习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();
}
}
的package.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'
]
}
};
HeroService.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));
}
}
答案 0 :(得分:65)
您需要导入\n\r
运算符:
\d{*}
更新rxjs&gt; = 5.5.0:
对于rxjs@5.5.0或更高版本,建议使用管道操作符而不是扩充:
switchMap
(这可避免副作用并实现更好的束大小优化)
答案 1 :(得分:7)
此问题已修复,请查看以下内容:
this.route.params.forEach((params: Params) => {
if (params['id'] !== undefined) {
let id = +params['id'];
this.heroService.getHero(id)
.then(hero => this.hero = hero);
}
});
答案 2 :(得分:6)
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
this.user = this.afAuth.authState.pipe(switchMap(user => {
if (user) {
return this.afs.doc<User>(`users/${user.uid}`).valueChanges()
} else {
return of(null)
}
}));
}
答案 3 :(得分:4)
您使用rxjs 6.x.x附带的Angular 6更新
import { switchMap } from 'rxjs/operators';
然后只需用管道操作符包装switchMap。
this.route.params.pipe(switchMap((params: Params) => {
this.heroService.getHero(+params['id'])
})).subscribe((hero: Hero) => this.hero = hero);
答案 4 :(得分:2)
import { ActivatedRoute, Params } from '@angular/router';
import { switchMap } from 'rxjs/operators';
...
ngOnInit(): void {
this.route.params.pipe(
switchMap(
(params: Params) =>
this.heroService.getHero(+params['id'])))
.subscribe(hero => this.hero = hero);
}
答案 5 :(得分:1)
我正在使用Visual Studio进行开发,并且也遵循Angular文档中的Angular2教程的相同内容:https://angular.io/docs/ts/latest/tutorial/toh-pt5.html
,即使我是switchMap
,我也会遇到相同的错误使用导入:import 'rxjs/add/operator/switchMap';
。
我意识到我的应用程序没有为我加载,因为.html
文件位于src/app
文件夹中 - 即使Angular教程告诉您将它们保留在该目录中。没有人告诉我这样做 - 只是偶然 - 我将hero-detail.component.html
和dashboard.component.html
移动到/src
文件夹,然后应用程序开始在浏览器中显示正确的结果。
答案 6 :(得分:1)
rxjs随附的Angular 7更新> 6.x.x您
从'rxjs / operators'导入{switchMap}; 然后,只需使用管道运算符将switchMap包装起来即可。
this.route.params.pipe(switchMap((params: Params) =>
this.heroService.getHero(+params['id'])))
.subscribe((hero: Hero) => {this.hero = hero});