2016-08-13 41 views
1

是否可以在指定的ui-router狀態下創建新的BrowserWindow?這裏是我的main.js在指定的UI路由器狀態下啓動的新電子BrowserWindow

const {app, BrowserWindow, Menu} = require('electron') 

let win 

function createWindow() { 
    win = new BrowserWindow({ width: 950, height: 660 }) 
    win.loadURL(`file://${__dirname}/index.html`) 

    // Emitted when the window is closed. 
    win.on('closed',() => { win = null }) 

    app.dock.setMenu(Menu.buildFromTemplate([ 
    { label: 'Open at page', click:() => { goToState('page') } } 
    ])) 
} 

let goToState = (state) => { 
    if (!win) { 
    createWindow() 
    // when win is ready, call win.webContents.send('sref', state) 
    } else { 
    win.webContents.send('sref', state) 
    } 
} 

app.on('ready', createWindow) 

app.on('activate',() => { 
    if (win === null) { 
    createWindow() 
    } 
}) 

'sref'事件執行在渲染過程中$state.go(state)

我已經嘗試過:

  • 傳遞函數爲回調createWindow()

    function createWindow (callback) { 
    
        ... 
    
        if (callback) callback() 
    } 
    
    let goToState = (state) => { 
        if (!win) { 
        createWindow(() => { 
         win.webContents.send('sref', state) 
        }) 
        } else { 
        win.webContents.send('sref', state) 
        } 
    } 
    
  • 使用app.on('web-contents-created')

    let goToState = (state) => { 
        if (!win) { 
        createWindow() 
        app.on('web-content-created',() => { 
         win.webContents.send('sref', state) 
        }) 
        } else { 
        win.webContents.send('sref', state) 
        } 
    } 
    

感謝您的閱讀。

回答

1

你嘗試只設置符合國家的URL哈希後:

win.loadURL(`file://${__dirname}/index.html#/page`) 

假設你使用網址配置UI路由器狀態:

"url":"/page" 
+0

謝謝你,因爲國家名稱等於他們的url,這個解決方案對我的應用來說已經足夠了。我在'createWindow()'中添加了'stateUrl'作爲參數。 – xAlien95