我有以下代码,我用它来创建赌场管理系统:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { SearchService } from './search.service';
import { Data } from './datatypings';
@Component({
moduleId: module.id,
selector: 'casino-search',
templateUrl: 'search.component.html',
styleUrls: [ 'search.component.css' ],
providers: [SearchService]
})
export class SearchComponent implements OnInit {
data: Observable<Data[]>;
private searchTerms = new Subject<String>();
constructor(
private searchService: SearchService,
private router: Router) {}
// Push a search term into the observable stream
search(term: string): void {
this.searchTerms.next(term);
}
ngOnInit(): void {
this.data = this.searchTerms
.debounceTime(150) // wait for 150ms pause in events
.distinctUntilChanged() // ignore if next search term is same as previous
.switchMap(term => term // switch to new observable each time
// return the http search observable
? this.searchService.search(term)
// or the observable of empty data if no search term
: Observable.of<Data[]>([]))
.catch(error => {
// TODO: real error handling
console.log(error);
return Observable.of<Data[]>([]);
});
}
gotoDetail(data: Data): void {
let link = ['/detail', data.id];
this.router.navigate(link);
}
}
但是,当我尝试将此代码从TypeScript转换为JavaScript时,我收到此错误:
app/data/search.component.ts(37,37): error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'.
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
现在,我已经修改了错误,只需将角色括号中的String
更改为string
即可,但我希望将来能够继续操作而不会产生类似的错误,并且还要写出最好的错误代码我可以......所以我想知道是否有人可以为我解决这个问题:
我想我明白带有包装类的参数是封装原始类并提供某种额外的,通常是相互关联的行为的东西(如果我在这里错了,请纠正我),我明白为什么它可能然后无法将更基本的声明变量类型的内容分配给更简单的原语,错误消息的另一个方面让我感兴趣,即:
为什么最好在Angular2 / Typescript中使用类型'String'类型'string',这样一般意义上的错误似乎意味着什么?是什么让这个优惠?
答案 0 :(得分:7)
private searchTerms = new Subject<String>();
将其更改为
private searchTerms = new Subject<string>();
String
是 class
, string
是 {{1 }} 强>