似乎我无法启动并运行角度为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),);
}
}
是什么导致这种情况发生?
答案 0 :(得分:2)
您需要在构造函数
中注入服务 <强> constructor(private myservice:CoreService)
强>
然后再做,
this.myservice.query(login','POST',{username: this.username, password: this.password});
答案 1 :(得分:1)
您正试图致电&#34;查询&#34;方法作为静态方法。
如果你想这样打电话,请在此之前添加static
。
static query(action,type,postdata)
OR
在constructor(yourService: CoreService)
中注入您的服务并执行:
this.yourService.query(....);