javascript - 服务的调用功能。 Angular 6

标签 javascript angular

我似乎无法在 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/

相关文章:

Angular2 构建失败,出现 "Cannot find name ' 文档'”;其他引用也无法解析

Angular Form with ng-content -> form.valid/dirty 等不工作

javascript - 从 JavaScript 值分配模型属性值

javascript - 显示我使用 @ContentChildren 或 @ViewChild 获取的组件的内容

javascript - 使用 webpack 包含 Bootstrap

javascript - Angular 力矩 : Moment Timezone has no data for America/New_York

css - 在 Angular-material 中将 more_vert 按钮对齐右侧

angular - Storybook 在 Angular Intershop 项目中不起作用

javascript - 如何在 Apache Web 服务器上部署 React App

javascript - 使用 webpack 将构建文件复制到另一个元素