对于Angular js来说是新手,试图从youtube中学习它,我
在我的代码中遇到了提到的问题,我不明白我的代码中缺少什么
我正在尝试访问HttpClientModule的获取服务时出现"属性'获取'不存在类型'HttpClientModule'"错误.
以下是代码详细信息
app.component.ts文件是
import { Component,OnInit } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private http:HttpClientModule){};
title = 'app';
ngOnInit() {
this.http.get('https://my-json-server.typicode.com/techsithgit/json-faker-
directory/profiles').
subscribe((data)=>{
console.log(data);
});
}
}
Run Code Online (Sandbox Code Playgroud)
app.module.ts文件是
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: …
Run Code Online (Sandbox Code Playgroud) 我有两个相同类型的 Observable,我想将它们合并为一个 Observable 并删除重复项。
到目前为止,我基本上尝试了所有 rxjs 运算符,但没有一个能满足我的需要。请参阅附图:许多 rxjs 运算符会更改结果可观察值的类型。
const a: Observable<Foo[]> = ...
const b: Observable<Foo[]> = ...
Run Code Online (Sandbox Code Playgroud)
这是我检索一些数据的两次调用。
现在最接近的两个操作:
combineLatest(a, b);
Run Code Online (Sandbox Code Playgroud)
这个创建了一个具有两个索引的新数组,a 的结果将进入索引 0,b 的结果将进入索引 1。我不能这样使用它。
const c = a.pipe(mergeMap(a1 => b.pipe(map(a2 => [...a1, ...a2]))));
Run Code Online (Sandbox Code Playgroud)
这个差一点就得到了。这里的问题如下:假设 a 返回 2 个结果(示例值:1 和 2),b 也返回 2 个结果(示例值:2 和 3)。因此“2”是重复的,它不应在结果可观察值中出现两次。
有谁知道运算符的组合,以便我可以组合两个可观察值并删除重复项?
我有一个带有控制器和服务的 Nestjs 休息服务器。
在我的控制器中,当有人发出 get 请求时,有 get 函数:
@Get()
getAllFoos() {
return this.fooService.getAllFoos();
}
Run Code Online (Sandbox Code Playgroud)
在我的服务中,有一个从数据库获取文档的功能
async getAllFoos(): Promise<foos[]> {
try {
return await this.fooModel.find().exec();
} catch(e) {
return e;
}
Run Code Online (Sandbox Code Playgroud)
这有效!我现在需要更改它以使其与可观察量一起使用。我将控制器更改为:
@Get()
getAllFoos() {
this.fooService.getAllFoos().subscribe(
response => {
console.log(response);
},
error => {
console.log(error);
},
() => {
console.log('completed');
});
}
Run Code Online (Sandbox Code Playgroud)
以及对此的服务:
getAllFoos(): Observable<foos[]> {
try {
this.fooModel.find().exec();
} catch(e) {
return e;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
[Nest] 7120 - 2019-2-20 15:29:51 [ExceptionsHandler] Cannot read property 'subscribe' of undefined +4126ms
Run Code Online (Sandbox Code Playgroud)
错误来自 …