对于2.0的窗口创建,有几种方法,这一次以JS调用的方法为例:
这个在官网中存在示例:https://tauri.app/reference/javascript/api/namespacewebviewwindow/#new-webviewwindow
import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
const webview = new WebviewWindow('my-label', {
url: 'https://github.com/tauri-apps/tauri'
});
webview.once('tauri://created', function () {
// webview successfully created
});
webview.once('tauri://error', function (e) {
// an error happened creating the webview
});
此时直接运行不会存在任何窗口创建,同时也不会存在任何错误提示,理论上应该出现一个新窗口并且显示github的网页。哪里出问题了呢?
让我们在第8行的方法中先添加一个简单的异常打印:
//其他代码
webview.once('tauri://error', function (e) {
console.log(e)
});
//......
重新运行,在控制台应该可以看到错误提示:"webview.create_webview_window not allowed. Permissions associated with this command: core:webview:allow-create-webview-window",这个提示已经明说了,需要core:webview:allow-create-webview-window这个权限。
我们要清楚,所有js需要用到rust后端的操作,都需要配置权限;同时,tauri1.0的权限分配是allowlist,而2.0的权限分配变成了capabilities。这个在版本说明中有提到:https://tauri.app/blog/tauri-20/#the-allowlist-is-dead-long-live-the-allowlist。
因此,我们在tauri.conf.json中添加:
"app": {
"security": {
"capabilities": [{
"identifier": "my-capability",
"description": "My application capability used for all windows",
"windows": ["*"],
"permissions": [
"core:webview:allow-create-webview-window" // 添加这个
]
}]
},
"windows": [{
"title": "mms_gui_2",
"width": 800,
"height": 600
}]
//其他代码...
对于如何添加权限,有什么权限可以添加,可以在官网的这个地方查询到:https://tauri.app/reference/acl/core-permissions/
最后吐槽一个,查什么都不如查官网文档好啊。