本地主机
通过本地服务器而不是默认的自定义协议公开应用的资源。
¥Expose your app’s assets through a localhost server instead of the default custom protocol.
:::caution 提醒
此插件带来了相当大的安全风险,只有在你知道自己在做什么的情况下才应使用它。如果有疑问,请使用默认的自定义协议实现。
¥This plugin brings considerable security risks and you should only use it if you know what you are doing. If in doubt, use the default custom protocol implementation.
:::
¥Supported Platforms
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | | |
| ios | |
¥Setup
安装本地主机插件即可开始使用。
¥Install the localhost plugin to get started.
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
npm run tauri add localhostyarn run tauri add localhostpnpm tauri add localhostdeno task tauri add localhostbun tauri add localhostcargo tauri add localhost-
Run the following command in the
src-taurifolder to add the plugin to the project’s dependencies inCargo.toml:cargo add tauri-plugin-localhost -
Modify
lib.rsto initialize the plugin: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");}
¥Usage
localhost 插件在 Rust 中可用。
¥The localhost plugin is available in 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");}Tauri v2.9 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站