[英]How to fix an issue when window in undefined in tauri?
I am new at tauri and I have faced the issue with getting data from @tauri-apps/api.我是 tauri 的新人,我遇到了从@tauri-apps/api 获取数据的问题。
"@tauri-apps/api": "^1.1.0",
"@tauri-apps/cli": "^1.1.1"
This is my React code below:下面是我的 React 代码:
/index.jsx
import {getTauriVersion} from "@tauri-apps/api/app"
function App() {
const func = async () => {
const res = await getTauriVersion()
return res
}
return (<></>)
}
This is my tauri.conf.json这是我的 tauri.conf.json
{
"build": {
"beforeDevCommand": "npm run dev",
"beforeBuildCommand": "npm run build",
"devPath": "http://localhost:1420",
"distDir": "../dist",
"withGlobalTauri": true
},
...
"tauri": {
"allowList": {"all": true}
}
}
And the error is:错误是:
Uncaught (in promise) TypeError: window.__TAURI_IPC__ is not a function
unhandledRejection: ReferenceError: window is not defined
at o (file:///C:/test/test/node_modules/@tauri-apps/api/tauri-
a4b3335a.js:1:100)
The typical 2 sources for this kind of error are:此类错误的典型 2 个来源是:
You can write a function to judge可以写个function来判断
const handleIsTauri = () => {
return Boolean(
typeof window !== 'undefined' &&
window !== undefined &&
window.__TAURI_IPC__ !== undefined
)};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.