Angular 在角度6分量错误中声明模型

Angular 在角度6分量错误中声明模型,angular,typescript,mvvm,Angular,Typescript,Mvvm,我试图在我的角度组件中注入并声明一个模型。它似乎工作正常(我可以console.log模型并按预期打印),但TypeScript抛出了一个错误,说,预期有12个参数,但得到了0,好像它无法识别所有参数。我怎样才能解决这个问题 我的组件看起来像: import { Component, OnInit, HostListener } from '@angular/core'; import { BaseComponent } from '../base.component'; import { Si

我试图在我的角度组件中注入并声明一个模型。它似乎工作正常(我可以console.log模型并按预期打印),但TypeScript抛出了一个错误,说,
预期有12个参数,但得到了0
,好像它无法识别所有参数。我怎样才能解决这个问题

我的组件看起来像:

import { Component, OnInit, HostListener } from '@angular/core';
import { BaseComponent } from '../base.component';
import { SiteModel } from '../../models/site-model'

@Component({
  selector: 'app-create-site',
  templateUrl: './create-site.component.html',
  styleUrls: ['./create-site.component.css'],

})
export class CreateSiteComponent extends BaseComponent implements OnInit {
  data_ready;
  public newsite: SiteModel;

  constructor(private SiteService: SiteService, private authService: AuthService) { 
    super()
    this.newsite = new SiteModel(); 
    //error is under new SiteModel()
   }

  ngOnInit() {
    console.log(SiteModel)
  }
我的site-model.ts文件:

export class SiteModel {
    constructor(
        public id: string,
        public name: string,
        public address: string,
        public address2: string,
        public state: string,
        public zip: string,
        public note: string,
        public latitude: Number,
        public longitude: Number,
        public timezone: string,
        public customerId: string,
        public enabled: boolean
    ) {}
}
我可能会错过什么?
非常感谢

执行
new SiteModel()
后,您将运行构造函数。您提供的构造函数需要代码段中的所有参数,例如
id
address

您可以通过编写
public id?:string
使这些参数成为可选参数。请注意,在可选输入之后不能有非可选输入,例如
构造函数(id?:string,address:string)
。如果使所有参数都是可选的,则可以按照定义构造函数的相同顺序提供任意数量的参数,甚至不提供任何参数


对于角度组件,构造函数参数由依赖项注入(DI)处理。DI系统必须知道参数的类型并自动解析它。您可以通过
NgModule
中的
声明和
提供者
数组告诉Angular使用DI系统中的项目。您可以使用界面创建模型

// your model.ts file:

export interface IModel {
 id: string;
 name: string;
 .....
}
在您的组件中

import { Component, OnInit, HostListener } from '@angular/core';
import { BaseComponent } from '../base.component';
import { IModel } from '../../models/model'

@Component({
  selector: 'app-create-site',
  templateUrl: './create-site.component.html',
  styleUrls: ['./create-site.component.css'],

})
export class CreateSiteComponent extends BaseComponent implements OnInit {
  data_ready;
  public newsite: IModel;

  ......

给出类构造函数的默认值

site-model.ts文件:

export class SiteModel {
    constructor(
        public id: string,
        public name: string,
        public address: string,
        public address2: string,
        public state: string,
        public zip: string,
        public note: string,
        public latitude: Number,
        public longitude: Number,
        public timezone: string,
        public customerId: string,
        public enabled: boolean
    ) {}
}

在构造器上方执行以下操作。它应该在以下方面发挥作用:

public newsite: SiteModel= {

    id = '',
    name = '',
    address = '',
    address2 = '',
    longitude = 0
    ...
    ...
    ...

    }

注意:如果它是数字,则如果要使用
new SiteModel()创建实例,则应将0分配给它,而不是“”

-所以没有参数。。。 最简单的解决方案:将构造函数留空/或完全忽略构造函数

export class SiteModel {
        public id: string,
        public name: string,
        public address: string,
        public address2: string,
        public state: string,
        public zip: string,
        public note: string,
        public latitude: Number,
        public longitude: Number,
        public timezone: string,
        public customerId: string,
        public enabled: boolean = true // you can define defaults if you want

        constructor() {} // constructor is optional 
}
´´´