自动启动
在系统启动时自动启动您的应用程序。
支持的平台
此插件需要至少 1.77.2 版本的 Rust
平台 | 级别 | 注释 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
设置
安装 autostart 插件开始使用。
使用你的项目包管理器添加依赖项
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
用法
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