首页 > 解决方案 > TypeError:无法读取 TypeScript 生成的 JavaScript 文件上未定义的属性“推送”

问题描述

TypeError: Cannot read property 'push' of undefined当我运行我的应用程序时,我得到了。我正在用 TypeScript 编写并编译为ES5

打字稿文件

// Holds all windows
private windows!: [string,BrowserWindow][];

/**
 * Creates a browser window and returns it.
 * Note: The window will auto .show() when returned
 * 
 * @param windowView View name to be loaded
 * @param windowOptions BrowserWindow options 
 */
public createWindow(windowView:string, windowOptions?:BrowserWindowConstructorOptions): BrowserWindow {

    // Instantiates the BrowserWindow
    let window = new BrowserWindow(windowOptions)

    // Loads the view into the BrowserWindow
    window.loadFile(this.VIEWS_PATH + '/' + windowView + this.VIEWS_EXTENSION)

    // Adds view and window to BrowserWindow tupple array for futher use
    this.windows.push([windowView, window])

    // Closes and deletes this specific BrowserWindow from the BrowserWindow tupple array
    window.on('close', (event) => {
      console.log('deleted', event.sender.id)
      this.deleteWindow(undefined, event.sender.id)
    })

    return window
}

TypeScript 生成的 JavaScript 文件

/**
 * Creates a browser window and returns it.
 * Note: The window will auto .show() when returned
 *
 * @param windowView View name to be loaded
 * @param windowOptions BrowserWindow options
 */
createWindow(windowView, windowOptions) {
    // Instantiates the BrowserWindow
    let window = new electron_1.BrowserWindow(windowOptions);
    // Loads the view into the BrowserWindow
    window.loadFile(this.VIEWS_PATH + '/' + windowView + this.VIEWS_EXTENSION);
    // Adds view and window to BrowserWindow tupple array for futher use
    this.windows.push([windowView, window]);
    // Closes and deletes this specific BrowserWindow from the BrowserWindow tupple array
    window.on('close', (event) => {
        console.log('deleted', event.sender.id);
        this.deleteWindow(undefined, event.sender.id);
    });
    return window;
}

TypeScript 没有在 JavaScript 文件上定义 windows 元组数组,是因为它没有在 TypeScript 文件上初始化吗?任何解决方法?

非常感谢。

标签: javascripttypescript

解决方案


试试private windows!: [string,BrowserWindow][]=[]; (:P)


推荐阅读