当前位置: 首页 > 知识库问答 >
问题:

如何使用 TypeScript 在 Angular 2 组件中声明模型类?

晁文斌
2023-03-14

我是Angular 2和TypeScript的新手,我正在尝试遵循最佳实践。

我试图创建一个TypeScript类,而不是使用简单的JavaScript模型({ })。

但是,Angular 2好像不喜欢。

我的代码是:

import { Component, Input } from "@angular/core";

@Component({
    selector: "testWidget",
    template: "<div>This is a test and {{model.param1}} is my param.</div>"
})

export class testWidget {
    constructor(private model: Model) {}
}

class Model {
    param1: string;
}

我将其用作:

import { testWidget} from "lib/testWidget";

@Component({
    selector: "myComponent",
    template: "<testWidget></testWidget>",
    directives: [testWidget]
})

我收到来自 Angular 的错误:

异常:无法解析testWidget的所有参数:(?).

所以我想,模型还没有定义...我把它移到最上面!

除了现在我得到例外:

原始异常:模型没有提供程序!

我该如何做到这一点??

编辑:感谢大家的回答。它把我引向了正确的道路。

为了将它注入到构造函数中,我需要将它添加到组件的提供者中。

这似乎行得通:

import { Component, Input } from "@angular/core";

class Model {
    param1: string;
}

@Component({
    selector: "testWidget",
    template: "<div>This is a test and {{model.param1}} is my param.</div>",
    providers: [Model]
})

export class testWidget {
    constructor(private model: Model) {}
}

共有3个答案

伊羽
2023-03-14
export class Car {
  id: number;
  make: string;
  model: string;
  color: string;
  year: Date;

  constructor(car) {
      {
        this.id = car.id;
        this.make = car.make || '';
        this.model = car.model || '';
        this.color = car.color || '';
        this.year = new Date(car.year).getYear();
      }
  }
}

对于非常复杂的数据对象,||对于不存在的默认数据非常有用。

. .

在组件中。ts或服务。ts文件,您可以将响应数据反序列化到模型中:

// Import the car model
import { Car } from './car.model.ts';

// If single object
car = new Car(someObject);

// If array of cars
cars = someDataToDeserialize.map(c => new Car(c));
闻梓
2023-03-14

问题在于您尚未将 Model 添加到引导程序(这将使它成为单例)或组件定义的提供程序数组中:

@Component({
    selector: "testWidget",
    template: "<div>This is a test and {{param1}} is my param.</div>",
    providers : [
       Model
    ]
})

export class testWidget {
    constructor(private model: Model) {}
}

是的,您应该在Component之上定义Model。但更好的办法是把它放在他自己的文件中。

但是,如果您希望它只是一个可以创建多个实例的类,最好只使用new

@Component({
    selector: "testWidget",
    template: "<div>This is a test and {{param1}} is my param.</div>"
})

export class testWidget {

    private model: Model = new Model();

    constructor() {}
}
施德运
2023-03-14

我会试试这个:

将您的模型拆分到一个名为< code>model.ts的单独文件中:

export class Model {
    param1: string;
}

将其导入到您的组件中。这将为您提供能够在其他组件中使用它的额外好处:

Import { Model } from './model';

在组件中初始化:

export class testWidget {
   public model: Model;
   constructor(){
       this.model = new Model();
       this.model.param1 = "your string value here";
   }
}

在html中适当地访问它:

@Component({
      selector: "testWidget",
      template: "<div>This is a test and {{model.param1}} is my param.</div>"
})

我想在回答中添加@PatMigliaccio的评论,因为适应最新的工具和技术很重要:

如果您使用的是< code>angular-cli,您可以调用< code>ng g class model,它会为您生成。模型被替换成你想要的名字。

 类似资料:
  • 我试图实现的是从声明类型数组。 使用enum,我可以这样做: 输出将是 我想通过这种方式实现类似的目标: 输出类似: 有什么想法吗?我尝试了和。不幸的是,它不起作用。

  • 我在这里查过了https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md这是TypeScript语言规范,但我找不到如何声明函数的返回类型。 我在下面的代码中展示了我所期望的: 我知道我可以用

  • 主要内容:CalcThirdPartyJsLib.js 文件代码:,Calc.d.ts 文件代码:,CalcTest.ts 文件代码:,CalcTest.js 文件代码:,实例TypeScript 作为 JavaScript 的超集,在开发过程中不可避免要引用其他第三方的 JavaScript 的库。虽然通过直接引用可以调用库的类和方法,但是却无法使用TypeScript 诸如类型检查等特性功能。为了解决这个问题,需要将这些库里的函数和方法体去掉后只保留导出类型声明,而产生了一个描述 JavaS

  • 今天,我意识到让数组像字典一样运行更有意义,这样我就可以通过名称而不是数字索引访问特定元素。我希望能够执行而不是(即使我声明了一个将submitButton映射到请求的索引的枚举)。 这是可行的,但我现在允许一堆欺骗我的代码库,这是很糟糕的。我希望对象知道,只有类型的东西才能放入其中。 我可以用这些字段声明一个类,因为我希望它们的数量在将来可以动态添加,所以下面的操作是不可行的。

  • 我在typescript中声明了一个全局变量,类似于:global。test=“something”我尝试这样做,我得到错误属性“test”在类型“Global”上不存在。

  • 我的TypeScript项目中有一个声明文件,如下所示: 这很好用,我可以在项目中的任何地方使用这个名称空间,而无需导入它。 我现在需要从第三方模块导入一个类型并在我的环境声明中使用它: 编译器现在抱怨它找不到名称空间“MyApp”,可能是因为导入阻止了它处于环境中。 是否有一些简单的方法可以在使用第三方类型的同时保留声明的环境性?