如何在角度组件上声明一个接口?

时间:2017-01-09 14:37:32

标签: angularjs typescript

我有一个组件,其方法可以从表单接收值。 我想用界面描述表单数据。

但是,在运行时(ng serve),编译器告诉我接口是未知的。 (Public property 'friendshipFormModel' of exported class has or is using private name 'IFriendshipFormModel'.

如何正确声明接口?如果可能的话,我会避免为这个接口创建一个单独的文件,因为它属于组件。

文件:

import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import * as moment from 'moment';
import { FriendshipModel } from '../models/friendship.model';

interface IDatePickerDateModel {
    day: string;
    month: string;
    year: string;
    formatted: string;
    momentObj: moment.Moment;
}

interface IFriendshipFormModel {
    name: string;
    meetingDate?: IDatePickerDateModel;
}

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

export class CreateFriendshipComponent {
    @Output() friendshipCreated = new EventEmitter<FriendshipModel>();
    friendshipFormModel: IFriendshipFormModel;

    constructor() {
        this.friendshipFormModel = {
            name: '',
            meetingDate: null
        };
    }

    createFriendship() {
        const friendshipCreation: FriendshipModel = this.frienshipFactory(this.friendshipFormModel);
        this.friendshipCreated.emit(friendshipCreation);
    }
}

谢谢!

1 个答案:

答案 0 :(得分:2)

在这种情况下,只需导出接口

export interface IDatePickerDateModel {
    day: string;
    month: string;
    year: string;
    formatted: string;
    momentObj: moment.Moment;
}

export interface IFriendshipFormModel {
    name: string;
    meetingDate?: IDatePickerDateModel;
}