解决方案(尽管我仍然喜欢解释)
如果我在preload.ts文件的TOP中直接包含接口代码(不导入),则一切正常。我可以像预期的那样投射对象。
我有一个电子应用程序编译为打字稿。如启动器example here所示,我有一个main.ts和一个preload.ts。当我将文件导入preload.ts时,应用会在使用导入的位置崩溃。我在这里做错了什么?
错误提示:
api
未定义
这是完全不正确的,因为如果在以下标记的行中删除了CandleRes
导入,则代码可以正常工作。
经过一些测试,我发现:
import语句本身似乎不会引起问题。当我删除导入并将接口直接放在文件中时,我仍然遇到相同的错误
当我通过复制并粘贴preload.ts文件(位于底部)来包含界面时,文件崩溃了,无论我是否对界面进行任何引用
main.ts
import { app, BrowserWindow } from "electron";
import * as path from "path";
let mainWindow: Electron.BrowserWindow;
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js")
},
width: 800
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, "../index.html"));
}
app.on("ready", createWindow);
app.on("activate", () => {
// On OS X it"s common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
preload.ts
import { CandleRes } from "./models"; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<THIS LINE CAUSES CRASH
let api: any;
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener("DOMContentLoaded", async () => {
replaceText("colnames", "Loading data...."); //<<<<<<<<<<THIS FIRES CORRECTLY
setupApi(); //Omitted to save space. This inits the var api
await getDataAsync();
});
async function getDataAsync() {
try {
let candles: CandleRes = ( //<<<<<<<<<<<<<<<<<<<<<<<<<<<<I attempt to use import here. App crashes
await this.api.getCandles("USD_CAD", {
granularity: "M5",
count: 1
})
).data;
replaceText("currencies", candles + " string: " + JSON.stringify(candles));
} catch (e) {
replaceText("currencies", "currencies ERR: " + e);
}
}
const replaceText = (selector: string, text: string) => {
const element = document.getElementById(selector);
if (element) {
element.innerText = selector + ":" + text;
}
};
function setupApi() {
var key = "xxxxxxxxxxxxxxxxxxxxxxx"; //https://www.oanda.com/demo-account/tpa/personal_token
var acctid = "xxx-xxx-xxx-xxx"; //https://www.oanda.com/demo-account/funding
const Oanda = require("oanda-node-api");
const config = {
env: "fxPractice",
auth: `Bearer ${key}`,
accountID: acctid,
dateFormat: "RFC3339"
};
this.api = Object.create(Oanda);
this.api.init(config);
}
models.ts
export interface OHLC {
o: string; // "1.34288"
h: string; //"1.34336"
l: string; //1.34278"
c: string; //1.34315"
}
export interface Candle {
complete: boolean; //true
volume: number; //283
time: string; //2020-02-28T17:50:00.000000000Z
mid: OHLC;
}
//{"instrument":"USD_CAD","granularity":"M5",
//"candles":[
//{"complete":true,"volume":90,"time":"2020-02-28T21:55:00.000000000Z",
//"mid":{"o":"1.33974","h":"1.33974","l":"1.33932","c":"1.33968"}}]}
export interface CandleRes {
instrument: string; //ex: "USD_CAD"
granularity: string; //ex "M5"
candles: Candle[];
}
答案 0 :(得分:1)
您在全局范围内声明了@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class MyCheckValidator implements ConstraintValidator<MyCheck, Object[]> {
@Override
public boolean isValid(
Object[] value,
ConstraintValidatorContext context) {
if( value.length <2 ) {
return true;
}
if ( ! value[0].equals(value[1].myField()) {
return false;
}
return true;
}
}
变量,并且您尝试使用api
在函数内部访问它,这是不正确的。只要看看电子示例在this.api
中如何使用mainWindow
。
您在main.ts
中执行一次,在getDataAsync()
中进行两次。将您所有的setupApi()
更改为this.api
,这个特殊的问题就应该解决了。
如果您想扩展该领域的知识,请阅读以下精彩文章:'this' in TypeScript