跳转到内容
Tauri

Localhost

GitHub crates.io
API Reference

通过 localhost 服务器而不是默认的自定义协议公开你的应用资源。

支持的平台

  • Windows
  • Linux
  • macOS

设置

这个插件要求 Rust 版本至少是 1.75

  1. 通过将以下内容添加到 Cargo.toml 中来安装 localhost 插件。
src-tauri/Cargo.toml
[dependencies]
tauri-plugin-localhost = "2.0.0-beta"
# 或者使用 Git:
tauri-plugin-localhost = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
  1. 修改 lib.rs 来初始化插件。
src-tauri/src/lib.rs
fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_localhost::Builder::new().build())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

用法

Rust 提供了 localhost 插件。

src-tauri/src/lib.rs
use tauri::{webview::WebviewWindowBuilder, WebviewUrl};
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");
}

© 2024 Tauri Contributors. CC-BY / MIT