自动启动
在系统启动时自动启动你的应用。
🌐 Automatically launch your application at system startup.
🌐 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 autostart plugin to get started.
使用项目的包管理器添加依赖:
npm run tauri add autostartyarn run tauri add autostartpnpm tauri add autostartdeno task tauri add autostartbun tauri add autostartcargo 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 客户端绑定:
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
🌐 Usage
自动启动插件在 JavaScript 和 Rust 中都可用。
🌐 The autostart plugin is available in both JavaScript and 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");}🌐 Permissions
默认情况下,所有潜在危险的插件命令和作用域都会被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用这些功能。
🌐 By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.
有关更多信息,请参见功能概览,有关使用插件权限的分步指南,请参见分步指南。
🌐 See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "permissions": [ ..., "autostart:allow-enable", "autostart:allow-disable", "autostart:allow-is-enabled" ]}Default Permission
This permission set configures if your application can enable or disable auto starting the application on boot.
Granted Permissions
It allows all to check, enable and disable the automatic start on boot.
This default permission set includes the following:
allow-enableallow-disableallow-is-enabled
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the disable command without any pre-configured scope. |
|
|
Denies the disable command without any pre-configured scope. |
|
|
Enables the enable command without any pre-configured scope. |
|
|
Denies the enable command without any pre-configured scope. |
|
|
Enables the is_enabled command without any pre-configured scope. |
|
|
Denies the is_enabled command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站