我似乎无法在 Angular 6 中启动并运行我的服务。我试图调用 CoreService 中的任何函数,但它似乎不起作用。
什么原因?
错误:
error TS2339: Property 'query' does not exist on type 'typeof CoreService'.
这是我调用 CoreService 函数的地方。
import { CoreService} from "./../core.service";
CoreService.query('login','POST',{username: this.username, password: this.password}).takeUntil(this.ngUnsubscribe).subscribe((value) => {
});
这是我的服务:
import { throwError as observableThrowError, interval as observableInterval, Observable, Subscription , Subject} from 'rxjs';
import { tap, map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { RequestOptions, URLSearchParams } from '@angular/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { environment } from './../environments/environment';
import { Router, NavigationEnd, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
@Injectable({providedIn: 'root'})
export class CoreService {
constructor(private http: HttpClient, private router: Router) {}
result = {};
permission = false;
baseurl = environment.apiUrl;
baseapi = this.baseurl + ':3000/api';
options = {withCredentials: true};
query(action,type,postdata) {
// console.log("calling: " + type + " : " + action);
if (type == 'POST') {
return this.http.post(this.baseapi + "/" + action, postdata ,this.options).pipe(
map(result => result),
tap(result => this.result = result),);
} else {
return this.http.get(this.baseapi + "/" + action, this.options).pipe(
map(result => result),
tap(result => this.result = result),);
}
}
performLogin(name: string, password: string) {
var params = new URLSearchParams();
params.append('username', name);
params.append('password', password);
return this.http.post(this.baseapi + '/login/login',params, this.options).pipe(
map(result => result),
tap(result => this.result = result),);
}
}
什么原因导致这种情况发生?
最佳答案
您需要将服务注入(inject)构造函数内
构造函数(私有(private)myservice:CoreService)
然后做,
this.myservice.query(login','POST',{username: this.username, password: this.password});
关于javascript - 服务的调用功能。 Angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50510310/