本地主机
通过本地主机服务器而非默认自定义协议公开应用程序资产。
此插件需要 Rust 版本至少为 **1.77.2**
| 平台 | 级别 | 备注 |
|---|---|---|
| Windows | ||
| Linux | ||
| macOS | ||
| Android | | |
| iOS | |
安装 localhost 插件即可开始使用。
使用你的项目包管理器添加依赖项
npm run tauri add localhostyarn run tauri add localhostpnpm tauri add localhostdeno task tauri add localhostbun tauri add localhostcargo 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!("https://:{}", 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 贡献者。CC-BY / MIT