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 参考
  • getPreferenceValues
  • openExtensionPreferences
  • openCommandPreferences
  • 类型
  • Preferences
  1. API 参考

Preferences

上一页OAuth下一页Storage

最后更新于1年前

使用 Preferences API 使您的扩展可配置。

Preferences 在每个命令的 中配置或在扩展的上下文中共享。

在打开命令之前,用户需要设置所需的首选项。它们是确保扩展程序的用户正确设置所有内容的好方法。

API 参考

getPreferenceValues

用于访问已传递给命令的首选项值的函数。

每个首选项名称都映射到其值,并且定义的默认值作为备用值。

签名

function getPreferenceValues(): { [preferenceName: string]: any };

例子

import { getPreferenceValues } from "@raycast/api";

interface Preferences {
  name: string;
  bodyWeight?: string;
  bodyHeight?: string;
}

export default async function Command() {
  const preferences = getPreferenceValues<Preferences>();
  console.log(preferences);
}

返回

一个对象,其中首选项名称作为属性键,键入的值作为属性值。

根据首选项的类型,其值的类型会有所不同。

首选项类型
值类型

textfield

string

password

string

checkbox

boolean

dropdown

string

appPicker

file

string

directory

string

openExtensionPreferences

打开扩展程序的首选项面板。

签名

export declare function openExtensionPreferences(): Promise<void>;

例子

import { ActionPanel, Action, Detail, openExtensionPreferences } from "@raycast/api";

export default function Command() {
  const markdown = "API key incorrect. Please update it in extension preferences and try again.";

  return (
    <Detail
      markdown={markdown}
      actions={
        <ActionPanel>
          <Action title="Open Extension Preferences" onAction={openExtensionPreferences} />
        </ActionPanel>
      }
    />
  );
}

返回

打开扩展首选项面板时,promise 为 resolve。

openCommandPreferences

打开命令的首选项面板。

签名

export declare function openCommandPreferences(): Promise<void>;

例子

import { ActionPanel, Action, Detail, openCommandPreferences } from "@raycast/api";

export default function Command() {
  const markdown = "API key incorrect. Please update it in command preferences and try again.";

  return (
    <Detail
      markdown={markdown}
      actions={
        <ActionPanel>
          <Action title="Open Extension Preferences" onAction={openCommandPreferences} />
        </ActionPanel>
      }
    />
  );
}

返回

打开命令的首选项面板时,promise 为 resolve。

类型

Preferences

根据首选项的类型,其值的类型会有所不同。

首选项类型
值类型

textfield

string

password

string

checkbox

boolean

dropdown

string

appPicker

file

string

directory

string

Raycast 提供了一个名为 Preferences 的全局 TypeScript 命名空间,其中包含扩展的所有命令的首选项类型。

例如,如果名为 show-todos 的命令有一些首选项,则可以使用 getPreferenceValues<Preferences.ShowTodos>() 指定其 getPreferenceValues 的返回类型。这将确保命令中使用的类型与 manifest 保持同步。

命令通过 函数接收其首选项的值。它是一个对象,其中首选项的名称作为键,其值作为属性的值。

manifest
getPreferenceValues
Application
Application