Tauri 傳遞參數(shù)

2023-10-20 17:47 更新

你可以像運行普通命令一樣向Sidecar命令傳遞參數(shù),如同運行普通命令一樣。首先,在 tauri.conf.json 中定義需要傳遞給Sidecar命令的參數(shù):

{
"tauri": { "bundle": { "externalBin": [ "/absolute/path/to/sidecar", "relative/path/to/binary", "binaries/my-sidecar" ] }, "allowlist": { "shell": { "sidecar": true, "scope": [ { "name": "binaries/my-sidecar", "sidecar": true, "args": [ "arg1", "-a", "--arg2", { "validator": "\\S+" } ] } ] } } } }

然后,要調用sidecar命令,只需將所有參數(shù)作為數(shù)組傳遞:

import { Command } from '@tauri-apps/api/shell'
// 或者使用 `window.__TAURI__.shell.Command` // `binaries/my-sidecar` 是在 `tauri.conf.json > tauri > bundle > externalBin` 中指定的確切值 // 請注意,參數(shù)數(shù)組必須與 `tauri.conf.json` 中指定的完全匹配。 const command = Command.sidecar('binaries/my-sidecar', [ 'arg1', '-a', '--arg2', '與驗證器匹配的任意字符串', ]) const output = await command.execute()

這將傳遞參數(shù)給Sidecar命令,并執(zhí)行它。


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號