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

后端 未结 8 696
野趣味
野趣味 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:32

    In your case you are having model on same page, but you have it declared after your Component class, so that's you need to use forwardRef to refer to Class. Don't prefer to do this, always have model object in separate file.

    export class testWidget {
        constructor(@Inject(forwardRef(() => Model)) private service: Model) {}
    }
    

    Additionally you have to change you view interpolation to refer to correct object

    {{model?.param1}}
    

    Better thing you should do is, you can have your Model Class define in different file & then import it as an when you require it by doing. Also have export before you class name, so that you can import it.

    import { Model } from './model';
    

提交回复
热议问题