Angular 返回角度为6的service.ts中未定义的值

Angular 返回角度为6的service.ts中未定义的值,angular,typescript,angular6,Angular,Typescript,Angular6,我正在学习angular,从服务获取数据时遇到了问题。这是我的密码 来自component.ts的代码 import { Component, OnInit, Input } from '@angular/core'; import { RegisterService } from '../services/register.service'; @Component({ selector: 'app-register', templateUrl: './register.co

我正在学习angular,从服务获取数据时遇到了问题。这是我的密码

来自component.ts的代码

 import { Component, OnInit, Input } from '@angular/core';
 import { RegisterService } from '../services/register.service';

 @Component({
   selector: 'app-register',
   templateUrl: './register.component.html',
   styleUrls: ['./register.component.scss']
  })
  export class RegisterComponent implements OnInit {

  contact:string;

  constructor(private data : RegisterService) { }

  ngOnInit() {
  }

  onSubmit(f){
  console.log('value from component.ts',f.value)

  if(f.valid){
  // "username":username,
  // "email":email,
  // "password":password,
  // "c_password":c_password,
  // "contact":contact,
  // "type":"user"
  // }
  console.log('type',f.value.type)
this.data.register(f.value.name,f.value.email,f.value.password,f.value.c_password,f.value.conatct,f.value.type)
.subscribe(res => {
  console.log(res)});
   }
  }

}
问题是从服务获取数据

来自service.ts的代码为

 import { Injectable } from '@angular/core';
 import { HttpHeaders, HttpClient } from '@angular/common/http';
 import { Body } from '@angular/http/src/body';

 @Injectable({
 providedIn: 'root'
 })


 export class RegisterService {

 API_URL = "http://..";

 constructor(private http: HttpClient) { }

 register(name,email,password,c_password,contact,type){

  let body ={
    "name":name,
    "email":email,
    "password":password,
    "c_password":c_password,
    "contact":contact,
    "type": 'user'
    }
    console.log('body from service',body);

    const headers = new HttpHeaders({
      "Accept" : 'application/json',
      "Content-Type" : 'application/json'
    })

return this.http.post(this.API_URL+"register",body,{headers :headers});
    }
   }
这里的联系人在控制台中是未定义的返回。我是《天使6》的新手。控制台的屏幕截图添加到这里。

只需发送f.value并重试,而不是将每个值发送到服务。
检查控制台日志,您应该获得从component.ts传递到service.ts的所有数据。ts

问题是我没有拼写“f.value.contact”。现在一切正常

您在控制台中的错误是什么?它说的是错误401,这是关于您正在命中以进行注册的API的。@AnsBilal错误401是因为联系人未定义。just contact未定义?@AnsBilal yes only contact未定义undefined@AnsBilal感谢联系方式已获取,但显示401错误。。这个问题是因为数据类型吗??