這是我的代碼編譯時會拋出錯誤:打字稿錯誤:屬性「價值」並不在類型存在「可觀察<any>」
export class NoticeService {
public notice: Observable<any>;
private observer: any;
constructor(private translate: TranslateService) {
this.notice = new Observable(observer => {
this.observer = observer;
}).share();
}
create(value: string) {
let translatedValue = this.translate.get(value).value;
this.observer.next(translatedValue);
}
}
的console.log(this.translate.get(value))
輸出爲:
ScalarObservable {_isScalar: true, value: "Some proper value!", etc.
的輸出console.log(translatedValue)
是:
"Some proper value!"
錯誤是:
ERROR in [default] /somePath/notice.service.ts:21:52
Property 'value' does not exist on type 'Observable<any>'.
線路21:
let translatedValue = this.translate.get(value).value;
出了什麼問題?
更新:
我使用NG2-翻譯,這是get
方法:返回
/**
* Gets the translated value of a key (or an array of keys)
* @param key
* @param interpolateParams
* @returns {any} the translated key, or an object of translated keys
*/
TranslateService.prototype.get = function (key, interpolateParams) {
var _this = this;
if (!key) {
throw new Error('Parameter "key" required');
}
// check if we are loading a new translation to use
if (this.pending) {
return this.pending.map(function (res) {
return _this.getParsedResult(_this.parser.flattenObject(res), key, interpolateParams);
});
}
else {
var translations = void 0;
if (this.translations[this.currentLang]) {
translations = this.parser.flattenObject(this.translations[this.currentLang]);
}
return Observable_1.Observable.of(this.getParsedResult(translations, key, interpolateParams));
}
};
什麼是'TranslateService'的'GET'方法的內容?謝謝! –
謝謝您的回覆,我更新了我的問題。 –
謝謝!既然可觀察物被返回,你爲什麼不訂閱呢?就像這樣:'this.translate.get(value).subscribe(translatedValue => this.observer.next(translatedValue));' –