javascript - 错误 ReferenceError: countryCallingCode 未定义
问题描述
尝试为countryCallingCode
第一个选项中不存在的属性设置值时出现错误。
this.allData.customerFacingPhone.countryCallingCode = newItem.countryCallingCode
我收到错误:
ERROR ReferenceError: countryCallingCode is not defined
当尝试console.log(allData);
我没有customerFacingPhone
...我想创建 and 的属性allData customerFacingPhone
并.countryCallingCode
设置值...
解决方案
很可能您还没有声明类型。尝试声明如下类型
export SomeComponent {
allData: {
customerFacingPhone: {
countryCallingCode: any; // <-- elegant would be to define correct type here
}
} = Object.create(null); // <-- initializing empty object
// some example function
someFunction() {
this.allData.customerFacingPhone.countryCallingCode = newItem.countryCallingCode;
}
}
更优雅的方法是使用 TS 接口来定义类型。
数据.ts
export interface Data {
customerFacingPhone: Phone;
someOtherProperty: string;
}
export interface Phone {
countryCallingCode: string;
someOtherProperty: number;
}
并在控制器中使用
import { Data, Phone } from './data';
export SomeComponent {
allData: Data = Object.create(null); // <-- initializing empty object
// some example function
someFunction() {
this.allData.customerFacingPhone.countryCallingCode = newItem.countryCallingCode;
}
}
推荐阅读
- flutter - 如何使容器的填充可以点击?
- mysql - 在 mySQL 中从一个生成多行
- python-3.x - 如何将 s3 图像响应读取为对象
- scala - Spark - 拆分后如何将新列附加到原始数据帧?
- sql - 如何在 SQL 中设置 auto_increment 的初始值
- java - java.lang.StackOverflowError: null JsonWriter.writeDeferredName 但不使用 Json?
- sql - 在单独的列中添加关键字段的历史数据(以前的合同)
- sql - 对 IN 子句使用选择查询以及硬编码值
- neo4j - 从单个 csv 文件加载节点并创建关系
- r - Generating a boxplot with slightly darker lines then the fill color