Tauri 在JavaScript中訪問文件

2023-10-20 17:48 更新

在這個示例中,我們希望捆綁附加的 i18n JSON 文件,這些文件看起來像這樣:

de.json

{
"hello": "Guten Tag!",
"bye": "Auf Wiedersehen!"
}

在這種情況下,我們將這些文件存儲在 tauri.conf.json 旁邊的 lang 目錄中。為此,我們將 "lang/*" 添加到資源(resources),并將 $RESOURCE/lang/* 添加到 fs 范圍,就像上面所示的那樣。

請注意,您必須配置允許列表(allowlist),以啟用 path > all 和您需要的 fs API,在這個示例中是 fs > readTextFile。

import { resolveResource } from '@tauri-apps/api/path'
// alternatively, use `window.__TAURI__.path.resolveResource`
import { readTextFile } from '@tauri-apps/api/fs'
// alternatively, use `window.__TAURI__.fs.readTextFile`

// `lang/de.json` is the value specified on `tauri.conf.json > tauri > bundle > resources`
const resourcePath = await resolveResource('lang/de.json')
const langDe = JSON.parse(await readTextFile(resourcePath))

console.log(langDe.hello) // This will print 'Guten Tag!' to the devtools console


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號