首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >呼叫服务功能。Angular6

呼叫服务功能。Angular6
EN

Stack Overflow用户
提问于 2018-05-24 13:11:43
回答 2查看 778关注 0票数 0

它似乎无法使我的服务启动和运行的角度6。我试图调用任何函数在CoreService,但它似乎不工作。

原因是什么?

错误:

代码语言:javascript
复制
  error TS2339: Property 'query' does not exist on type 'typeof CoreService'.

这就是我调用CoreService函数的地方。

代码语言:javascript
复制
import { CoreService} from "./../core.service";

  CoreService.query('login','POST',{username: this.username, password: this.password}).takeUntil(this.ngUnsubscribe).subscribe((value) => {

        });

这是我的服务:

代码语言:javascript
复制
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),);
  }




}

是什么导致了这种情况?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-24 13:14:06

您需要在构造函数中注入服务。

constructor(private myservice:CoreService)

然后去做,

代码语言:javascript
复制
this.myservice.query(login','POST',{username: this.username, password: this.password});
票数 2
EN

Stack Overflow用户

发布于 2018-05-24 13:13:53

您正在尝试将"query“方法作为静态方法调用。

如果您想这样调用,请在前面添加static

代码语言:javascript
复制
static query(action,type,postdata)

将您的服务注入一个constructor(yourService: CoreService)中,然后执行:

代码语言:javascript
复制
this.yourService.query(....);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50510310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档