如果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]; } }