How do I declare a model class in my Angular 2 component using TypeScript?

后端 未结 8 701
野趣味
野趣味 2020-12-23 11:10

I am new to Angular 2 and TypeScript and I\'m trying to follow best practices.

Instead of using a simple JavaScript model ({ }), I\'m attempting to create a TypeScri

8条回答
  •  春和景丽
    2020-12-23 11:43

    create model.ts in your component directory as below

    export module DataModel {
           export interface DataObjectName {
             propertyName: type;
            }
           export interface DataObjectAnother {
             propertyName: type;
            }
        }
    

    then in your component import above as, import {DataModel} from './model';

    export class YourComponent {
       public DataObject: DataModel.DataObjectName;
    }
    

    your DataObject should have all the properties from DataObjectName.

提交回复
热议问题