Skip to content

File API 指南 ​

文件读写是插件最常见的操作。Vault API 提供完整的创建、读取、修改与删除能力,并自动触发元数据重建。

核心要点 ​

读取与写入 ​

ts
// 读取
const content = await app.vault.read(file);
// 写入(覆盖)
await app.vault.modify(file, newContent);
// 创建
const file = await app.vault.create("笔记.md", "内容");
// 删除
await app.vault.delete(file);

路径与抽象 ​

  • 文件用 TFile / TFolder 表示,path 为仓库相对路径
  • 用 app.vault.getAbstractFileByPath("a/b.md") 按路径取对象
  • 大文件建议流式或分块处理,避免阻塞主线程

监听变更 ​

ts
this.registerEvent(
  app.vault.on("modify", (file) => console.log("修改了", file.path))
);
  • 事件需在 onunload 前 registerEvent 注册,插件卸载自动清理

相关阅读 ​