自启动
在系统启动时自动启动您的应用程序。
支持的平台
此插件需要至少版本的Rust 1.77.2
平台 | 级别 | 备注 |
---|---|---|
Windows | ||
Linux | ||
macOS | ||
Android | | |
iOS | |
设置
安装自动启动插件开始。
使用项目包管理器添加依赖项
npm run tauri add autostart
yarn run tauri add autostart
pnpm tauri add autostart
deno task tauri add autostart
bun tauri add autostart
cargo tauri add autostart
-
在
src-tauri
文件夹中运行以下命令将插件添加到项目依赖项Cargo.toml
cargo add tauri-plugin-autostart --target 'cfg(any(target_os = "macos", windows, target_os = "linux"))' -
修改
lib.rs
来初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(desktop)]app.handle().plugin(tauri_plugin_autostart::init(tauri_plugin_autostart::MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]) /* arbitrary number of args to pass to your app */));Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
您可以使用您偏好的JavaScript包管理器安装JavaScript Guest绑定
npm install @tauri-apps/plugin-autostartyarn add @tauri-apps/plugin-autostartpnpm add @tauri-apps/plugin-autostartdeno add npm:@tauri-apps/plugin-autostartbun add @tauri-apps/plugin-autostart
使用方法
自动启动插件在JavaScript和Rust中均可用。
import { enable, isEnabled, disable } from '@tauri-apps/plugin-autostart';// when using `"withGlobalTauri": true`, you may use// const { enable, isEnabled, disable } = window.__TAURI__.autostart;
// Enable autostartawait enable();// Check enable stateconsole.log(`registered for autostart? ${await isEnabled()}`);// Disable autostartdisable();
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .setup(|app| { #[cfg(desktop)] { use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_autostart::ManagerExt;
app.handle().plugin(tauri_plugin_autostart::init( MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]), ));
// Get the autostart manager let autostart_manager = app.autolaunch(); // Enable autostart let _ = autostart_manager.enable(); // Check enable state println!("registered for autostart? {}", autostart_manager.is_enabled().unwrap()); // Disable autostart let _ = autostart_manager.disable(); } Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
权限
默认情况下,阻止所有潜在危险插件命令和作用域,无法访问。您必须修改 capabilities
配置中的权限才能启用这些。
有关更多信息,请参阅 能力概述 和 使用插件权限的逐步指南。
{ "permissions": [ ..., "autostart:allow-enable", "autostart:allow-disable", "autostart:allow-is-enabled" ]}
默认权限
此权限集配置了您的应用程序是否可以在启动时启用或禁用自动启动
授权权限
允许检查、启用和禁用启动时的自动启动
allow-enable
allow-disable
allow-is-enabled
权限表
标识符 | 描述 |
---|---|
|
启用无预配置作用域的禁用命令。 |
|
拒绝无预配置作用域的禁用命令。 |
|
启用无预配置作用域的启用命令。 |
|
拒绝无预配置作用域的启用命令。 |
|
启用无预配置作用域的is_enabled命令。 |
|
拒绝无预配置作用域的is_enabled命令。 |
© 2025 Tauri贡献者。CC-BY / MIT