简体   繁体   English

angular 4 typescript 验证错误对象文字可能只指定已知属性

[英]angular 4 typescript validation error Object literal may only specify known properties

my service我的服务

import {Account} from '../models/Account';

  export class AccountingService {
  domain: string = "http://localhost:3000/api";

  constructor(private http: HttpClient) {  }

  getAccounts(){
    return  this.http.get<Account[]>(`${this.domain}/accounts` )
              .map(res => res)
  }

  addAccount(newAccount:Account){
    return  this.http.post(`${this.domain}/accounts`,newAccount)
              .map(res=>res);
  }

  updateAccount(newAccount: Account){
    return this.http.put(`${this.domain}/accounts/${newAccount.id}`,newAccount)
              .map(res=>res);
  }

  deleteAccount(id){
    return  this.http.delete(`${this.domain}/accounts/${id}`)
              .map(res=>res);
  }
}

my model我的模特

export class Account{
    _id?: string;
    name: string;
    amount: number;

}

my component我的组件

import {AccountingService} from '../../services/accounting.service';

@Component({
  selector: 'app-accounting',
  templateUrl: './accounting.component.html',
  styleUrls: ['./accounting.component.css']
})
export class AccountingComponent implements OnInit {
  accounts:Account[];
  name:string;
  amount:number;
  constructor(private accountService : AccountingService ) {

    this.accountService.getAccounts()
      .subscribe(accounts =>{
        console.log(accounts);
      })

   }

   addAccount(event){
    event.preventDefault();
    const newAccount : Account={
      name: this.name,
      amount: this.amount
    };

    this.accountService.addAccount(newAccount);
   }

getAccounts() works perfectly but addAccount function give me a getAccounts() 工作得很好,但是 addAccount 函数给了我一个

error Object literal may only specify known properties and amount in does not exist in type Account错误对象字面量只能指定已知的属性,并且金额在类型 Account 中不存在

it may be a logical error but I do not know how to solve it in this moment这可能是一个逻辑错误,但我现在不知道如何解决它

Problem 1 - You didn't have imported Account interface in your AccountingComponent .问题 1 -您没有在AccountingComponent导入Account接口。

Add import { Account } from '../../models/Account';添加import { Account } from '../../models/Account'; in your AccountingComponent在您的AccountingComponent

Problem 2 - In your AccountingService the addAccount function have generic type <Account> , so you need to also define the type that you are returning from that method also as Account and not the default (which is any ).问题 2 -在您的AccountingServiceaddAccount函数具有泛型类型<Account> ,因此您还需要将从该方法返回的类型也定义为Account而不是默认值(即any )。 You can solve this by setting type of the res as Account .您可以通过将res类型设置为Account来解决此问题。

addAccount<Account>(newAccount:Account) {
    return this.http.post(`${this.domain}/accounts`,newAccount)
       .map((res: Account) => res);

} }

似乎您忘记了将您的服务设置为可注入的(并且可能没有在提供者列表中声明它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Object 文字只能指定已知属性 Angular/TypeScript - Object literal may only specify known properties Angular/TypeScript 错误:对象字面量只能以角度指定已知属性 - error: Object literal may only specify known properties in angular 对象文字只能指定已知属性 - Object Literal May Only Specify Known Properties 对象文字可能只指定已知属性错误 - Object literal may only specify known properties error 错误TS2322:对象文字可能仅指定已知属性,并且类型中不存在“标签” - error TS2322: Object literal may only specify known properties, and 'labels' does not exist in type 对象字面量只能指定已知的属性,并且类型“设置”中不存在“选择” - Object literal may only specify known properties, and 'select' does not exist in type 'Settings' 对象文字只能指定已知的属性,并且&#39;ChatMessage []&#39;类型中不存在&#39;message&#39; - Object literal may only specify known properties and 'message' does not exist in type 'ChatMessage[]' 类型的参数不能分配给类型的参数。对象文字只能指定已知的属性 - Argument of type is not assignable to parameter of type .Object literal may only specify known properties Object 文字可能只指定已知属性,并且“设置”类型中不存在“按钮” - Object literal may only specify known properties, and 'buttons' does not exist in type 'Settings Angular Object文字只能指定知道属性,而...在类型中不存在 - Angular Object literal may only specify know properties and … does not exist in type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM