angular2 中比较复杂的数据类型,在单独文件中定义。

前端之家收集整理的这篇文章主要介绍了angular2 中比较复杂的数据类型,在单独文件中定义。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果angular2书写不规范,那么界面中调用对象属性是会报错的,因为没有初始化变量属性,所以比较复杂的数据类型最好在单独文件重定义,然后引入初始化。
1、定义

export class DetailInfo {
    constructor(
        public userid: string,public userNm: string,public qqNm: string,public instid: string,public instfullnm: string,public instnm: string,public cfname: string,public ctname: string,public rgstMblph: string,public ctcMod1: string,public ctcMod2: string,public inptChnl: string,public dataeSource: string,public createtime: string,public lsttmusetm: string,public lbl: Array<string>,public udflbl: string,public wxopenid: string,public smbsnestatus: string,public qqvldestatus: string,public vldqq: string,public chkuname: string,public lsttmchktm: string,public state: string
    ) {}
}

2、使用

import {DetailInfo} from "./detail-info";
@Component({
    selector: "contacts-detail",templateUrl: "./contacts-detail.component.html",styleUrls: [
        "../contacts.component.css"
    ]
})
export class ContactsDetailComponent implements OnInit {
// 使用定义的数据类型
    detailInfo: DetailInfo;
    constructor() {
    
    }
    ngOnInit() {
    // 初始化
        this.detailInfo = {
            userid: "",userNm: "",qqNm: "",instid: "",instfullnm: "",instnm: "",cfname: "",ctname: "",rgstMblph: "",ctcMod1: "",ctcMod2: "",inptChnl: "",dataeSource: "",createtime: "",lsttmusetm: "",lbl: [],udflbl: "",wxopenid: "",smbsnestatus: "",qqvldestatus: "",vldqq: "",chkuname: "",lsttmchktm: "",state: ""
        };
    }
    selectValue(data: any) {
        // 这里就可以调用方法属性,不会报错,和界面是一样的
        this.detailInfo.instnm = this.detailInfo.instfullnm.split("--")[1];
        this.detailInfo.instid = this.detailInfo.instfullnm.split("--")[2];
        this.detailInfo.instfullnm = this.detailInfo.instfullnm.split("--")[0];
    }
}

猜你在找的Angularjs相关文章