Localhost
通过 localhost 服务器而不是默认的自定义协议公开你的应用资源。
支持的平台
- Windows
- Linux
- macOS
设置
- 通过将以下内容添加到 Cargo.toml中来安装 localhost 插件。
[dependencies]tauri-plugin-localhost = "2.0.0"# 或者使用 Git:tauri-plugin-localhost = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }- 修改 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 插件。
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中文网