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