【发布时间】:2020-07-05 10:11:55
【问题描述】:
我正在从 C# 转向打字稿,我想我可能正在尝试以错误的方式做某事。
我有这个界面:
export interface OilSpillInterface {
id?: number;
emergenceId?: number;
weather?: string;
insertOilSpill?: any;
}
我有这门课:
export default class OilSpillPrototype implements OilSpillInterface {
id?: number;
emergenceId?: number;
weather?: string;
constructor(
emergenceId?: number,
weather?: string){
this.emergenceId= emergenceId;
this.weather= weather;
}
insertOilSpill= async () => {
let _iar: ItemAddResult = await sp.web.lists
.getByTitle("Oil Spill")
.items.add({
emergenceId: this.emergenceId,
weather: this.weather
});
return _iar.data.ID != 0;
}
}
这就是我初始化“对象”的方式:
.
.
.
let _objOilSpill = new OilSpill(
this.props.emergenceId,
this.state.weather
);
console.log(_objOilSpill);
即使我不调用 insertOilSpill 方法,它也会执行。
我想我正在尝试以 C# 方式执行此操作,但即使我不调用它,我也不明白为什么它会执行。
我试图从界面中删除该方法,但它以任何方式执行。
关于我做错了什么的任何提示?
【问题讨论】:
-
你为什么相信
insertOilSpill会执行?您在此处编写的代码无法编译(至少没有称为“OilSpill”的类型),因此很难猜测其余代码是什么。您能否发布可编译的代码(例如在 typescriptlang.org/play 中)并演示您遇到的问题? -
举个例子,我看不到您所描述的问题,请参阅typescriptlang.org/play/…您看到我没有看到什么?
-
@RobNapier 感谢您的回复。我有一个带有初始化对象的提交函数的表单组件。 insertOilSpill 将对象插入到共享点列表中。如果我只是初始化对象(不调用插入函数),我可以看到在列表中创建的项目。我使用上面的链接放置了我拥有的所有代码:shorturl.at/fyGS5 谢谢!
-
还有“Inserindo item na lista de oil泄漏”日志?如果您注释掉
new OilSpillPrototype行,问题就消失了吗?我强烈怀疑问题出在其他地方。您将通过将其简化为重现问题的越来越小的事物来进行调试,直到某些事物无法重现为止,这将向您显示问题所在。闭包不会仅仅通过创建来执行;你在某处评估它。你只需要找到在哪里。 (您发布的代码实际上并没有显示问题,所以我希望确切的问题在您发布的代码之外的某个地方。) -
我发现了问题,我的组件在另一个表单组件里面,父组件的提交事件也被触发了。
标签: typescript ecmascript-6 frontend web-parts sharepointframework