Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在组件内部使用服务方法?_Angular_Service_Components - Fatal编程技术网

Angular 如何在组件内部使用服务方法?

Angular 如何在组件内部使用服务方法?,angular,service,components,Angular,Service,Components,我无法在组件内使用服务方法。我有一个服务和一个组件 组成部分 import { Component, OnInit } from '@angular/core'; import { Customer, Userdetails} from "./usermaster.model"; import { UsermasterService } from './usermaster.service'; @Component({ selector: 'ngx-usermaster', templ

我无法在组件内使用服务方法。我有一个服务和一个组件

组成部分

import { Component, OnInit } from '@angular/core';
import { Customer, Userdetails} from "./usermaster.model";
import { UsermasterService } from './usermaster.service';

@Component({
  selector: 'ngx-usermaster',
  templateUrl: './usermaster.component.html',
  styleUrls: ['./usermaster.component.scss'],
  providers:  [ UsermasterService ]
})
export class UsermasterComponent implements OnInit {
  values: any;
  UsermasterService: any;
  constructor(private service: UsermasterService) { }
  cust:Customer;
  user_details:Userdetails;
  ngOnInit() {
   this.cust={id:1,name:'dinesh'};
   console.log(this.cust);
   this.values = this.UsermasterService.get_data();
   console.log(this.values);
  }
 }
服务:

import { Injectable } from '@angular/core';
import { HttpClientModule } from  '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class UsermasterService {
  httpClient: any;

  constructor() { }

  get_data(){
    var array=[];
    array['id']='1';
    array['name']='dineshss';
    return array;
//     this.httpClient.get('http://localhost/tasker/api/index.php/Welcome/get_data')
// .subscribe(
//   (data:any[])=>{
//     console.log(data);
//   }
// )
  }

}

我需要在component.ts中调用方法get_data,当我运行代码时,我得到错误,无法读取未定义的属性get_data。请帮助我解决此问题。

要使用服务,您需要使用构造函数中用于注入服务的名称:

this.values = this.service.get_data()

要使用服务,您需要使用构造函数中用于注入服务的名称:

this.values = this.service.get_data()

因为在
UsermasterComponent
中,
此.UsermasterService
未定义。您将其声明为属性,但从不为其赋值。属性
UsermasterService
和类
UsermasterService
之间没有任何连接

带构造函数

  constructor(private service: UsermasterService) { }
constructor(private userMasterService: UsermasterService) { }

您应该以
this.service
的身份访问该服务,因为在
UsermasterComponent
中,
this.UsermasterService
未定义。您将其声明为属性,但从不为其赋值。属性
UsermasterService
和类
UsermasterService
之间没有任何连接

带构造函数

  constructor(private service: UsermasterService) { }
constructor(private userMasterService: UsermasterService) { }
您应该通过
此服务访问该服务。该服务涉及两个步骤

(i) 您需要将服务注入构造函数中

  constructor(private service: UsermasterService) { }
constructor(private userMasterService: UsermasterService) { }
(ii)调用并订阅该方法

this.values = this.userMasterService.get_data()
包括两个步骤,

(i) 您需要将服务注入构造函数中

  constructor(private service: UsermasterService) { }
constructor(private userMasterService: UsermasterService) { }
(ii)调用并订阅该方法

this.values = this.userMasterService.get_data()
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClientModule};
@注射的({
providedIn:'根'
})
导出类UserMasterService{
构造函数(私有http:HttpClient){}
//由于此服务是关于UserMaster的,
//不要将该方法命名为“get_data”,因为它太通用了。
//最好有一个单独的类“User”,以便
//类型是可观察的
getUsers():可观察的{
返回此.httpClient.get('http://localhost/tasker/api/index.php/Welcome/get_data');    
}    
}
导出类UserMasterComponent实现OnInit{
..........
构造函数(私有服务:UsermasterService){}
恩戈尼尼特(){
//其他源代码
//让组件决定如何处理返回的数据。
this.UserMasterService.getUsers().subscribe(用户=>{
这个值=用户;
console.log(this.values);
});
}
}
同样,正如我在其他问题中所建议的,请看一看像NSwag这样的代码生成工具,它可以自动生成这种类型的源代码(Xyz.service.ts)。

import{Injectable}from'@angular/core';
从'@angular/common/http'导入{HttpClientModule};
@注射的({
providedIn:'根'
})
导出类UserMasterService{
构造函数(私有http:HttpClient){}
//由于此服务是关于UserMaster的,
//不要将该方法命名为“get_data”,因为它太通用了。
//最好有一个单独的类“User”,以便
//类型是可观察的
getUsers():可观察的{
返回此.httpClient.get('http://localhost/tasker/api/index.php/Welcome/get_data');    
}    
}
导出类UserMasterComponent实现OnInit{
..........
构造函数(私有服务:UsermasterService){}
恩戈尼尼特(){
//其他源代码
//让组件决定如何处理返回的数据。
this.UserMasterService.getUsers().subscribe(用户=>{
这个值=用户;
console.log(this.values);
});
}
}
同样,正如我在其他问题中所建议的,请看一看像NSwag这样的代码生成工具,它可以自动生成这种类型的源代码(Xyz.service.ts)