Localhost
透過 localhost 伺服器而非預設的自訂協定,公開您應用程式的資源。
支援的平台
此外掛程式需要至少 1.77.2 的 Rust 版本
平台 | 層級 | 備註 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
設定
安裝 localhost 外掛程式以開始使用。
使用您專案的套件管理器新增依賴項
npm run tauri add localhost
yarn run tauri add localhost
pnpm tauri add localhost
deno task tauri add localhost
bun tauri add localhost
cargo tauri add localhost
-
在
src-tauri
資料夾中執行以下命令,將外掛程式新增至Cargo.toml
中專案的依賴項cargo add tauri-plugin-localhost -
修改
lib.rs
以初始化外掛程式src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_localhost::Builder::new().build()).run(tauri::generate_context!()).expect("error while running tauri application");}
用法
localhost 外掛程式在 Rust 中可用。
use tauri::{webview::WebviewWindowBuilder, WebviewUrl};
pub fn run() { let port: u16 = 9527;
tauri::Builder::default() .plugin(tauri_plugin_localhost::Builder::new(port).build()) .setup(move |app| { let url = format!("http://localhost:{}", port).parse().unwrap(); WebviewWindowBuilder::new(app, "main".to_string(), WebviewUrl::External(url)) .title("Localhost Example") .build()?; Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
© 2025 Tauri Contributors。CC-BY / MIT