Raycast 中文文档
  • 介绍
  • 链接
    • 官网
    • 官网 API 文档
    • 社区
    • GitHub
    • Store
    • Icon 生成器
    • 扩展图标模板
  • 基础
    • 起步
    • 创建您的第一个扩展
    • 贡献一个扩展
    • 过审一个扩展
    • 发布一个扩展
    • 调试一个扩展
    • 安装一个扩展
  • 团队
    • 开始
    • 发布私人扩展
    • 协作开发私有扩展
  • 例子
    • Doppler 共享 Secrets
    • Hacker News
    • Todo 列表
    • Spotify Controls
  • 资料
    • 最佳实践
    • 工具
      • CLI
      • ESLint
      • VS Code(社区工具)
    • 文件结构
    • 生命周期
      • 参数
      • 后台刷新
      • Deeplinks
    • Manifest
    • 安全性
    • 术语
    • 版本控制
  • API 参考
    • AI
    • Cache
    • Command
    • Clipboard
    • Environment
    • Feedback
      • Alert
      • HUD
      • Toast
    • Keyboard
    • Menu Bar Commands
    • OAuth
    • Preferences
    • Storage
    • System Utilities
    • 用户界面
      • Action Panel
      • Actions
      • Detail
      • Form
      • List
      • Grid
      • Colors
      • Icons & Images
      • Navigation
    • 窗口 & 搜索栏
  • 公共包
    • 起步
    • 功能
      • 执行 AppleScript
    • 图标
      • getAvatarIcon
      • getFavicon
      • getProgressIcon
    • React hooks
      • useCachedState
      • usePromise
      • useCachedPromise
      • useFetch
      • useForm
      • useExec
      • useSQL
      • useAI
  • 迁移
  • FAQ
由 GitBook 提供支持
在本页
  • API 参考
  • showToast
  • 类型
  • Toast
  • Toast.Options
  • Toast.Style
  • Toast.ActionOptions
  1. API 参考
  2. Feedback

Toast

上一页HUD下一页Keyboard

最后更新于1年前

当发生异步操作或引发错误时,通常最好让用户了解情况。Toast 就是为此而做的。

此外,Toast 可以有一些与其相关操作相关的操作。例如,您可以提供一种方法来取消异步操作、撤消操作或复制错误的堆栈跟踪。

API 参考

showToast

签名

async function showToast(options: Toast.Options): Promise<Toast>;

例子

import { showToast, Toast } from "@raycast/api";

export default async function Command() {
  const success = false;

  if (success) {
    await showToast({ title: "Dinner is ready", message: "Pizza margherita" });
  } else {
    await showToast({
      style: Toast.Style.Failure,
      title: "Dinner isn't ready",
      message: "Pizza dropped on the floor",
    });
  }
}

当显示动画 Toast 时,您可以稍后更新它:

import { showToast, Toast } from "@raycast/api";
import { setTimeout } from "timers/promises";

export default async function Command() {
  const toast = await showToast({
    style: Toast.Style.Animated,
    title: "Uploading image",
  });

  try {
    // upload the image
    await setTimeout(1000);

    toast.style = Toast.Style.Success;
    toast.title = "Uploaded image";
  } catch (err) {
    toast.style = Toast.Style.Failure;
    toast.title = "Failed to upload image";
    if (err instanceof Error) {
      toast.message = err.message;
    }
  }
}

参数

名称
描述
类型

options*

自定义 Toast 的选项。

返回

展示 Toast 状态为 resolves 的 Promise, Toast 可用于更改或隐藏它。

类型

Toast

具有特定样式、标题和消息的 Toast。

属性

名称
描述
类型

message*

Toast 的附加消息。有助于显示更多信息,例如新创建的资源的标识符。

string

primaryAction*

用户将鼠标悬停在 Toast 上时可以执行的 primary 操作。

secondaryAction*

用户将鼠标悬停在 Toast 上时可以执行的 secondary 操作。

style*

Toast 的样式。

title*

Toast 的标题,显示在顶部。

string

方法

名称
类型
描述

hide

() => Promise<void>

隐藏 Toast

show

() => Promise<void>

显示 Toast

Toast.Options

创建 Toast 的配置项。

例子

import { showToast, Toast } from "@raycast/api";

export default async function Command() {
  const options: Toast.Options = {
    style: Toast.Style.Success,
    title: "Finished cooking",
    message: "Delicious pasta for lunch",
    primaryAction: {
      title: "Do something",
      onAction: (toast) => {
        console.log("The toast action has been triggered");
        toast.hide();
      },
    },
  };
  await showToast(options);
}

属性

名称
描述
类型

title*

Toast 的标题,显示在顶部。

string

message

Toast 的附加消息。有助于显示更多信息,例如新创建的资源的标识符。

string

primaryAction

用户将鼠标悬停在 Toast 上时可以执行的 primary 操作。

secondaryAction

用户将鼠标悬停在 Toast 上时可以执行的 secondary 操作。

style

Toast 的样式。

Toast.Style

定义 Toast 的样式。

枚举成员

名称
值

Animated

Success

Failure

Toast.ActionOptions

用于创建 Toast 操作的选项。

属性

名称
描述
类型

title*

操作的标题。

string

onAction*

触发操作时调用的回调。

shortcut

操作的键盘快捷键。

创建并显示具有给定 的 Toast。

使用 创建并显示 Toast。

使用 进行确认,使用 显示错误。当您的 Toast 显示到进程完成时,请使用 。您可以稍后使用 Toast.hide 隐藏它或更新现有 Toast 的属性。

(toast: ) => void

选项
showToast
Toast.Style.Success
Toast.Style.Failure
Toast.Style.Animated
Toast
Keyboard.Shortcut
Alert.Options
Alert.ActionOptions
Alert.ActionOptions
Alert.ActionOptions
Alert.ActionOptions
Action.Style
Action.Style