1

我正在尝试为sanity.io. 为此,我创建了以下文档:

export default {
  name: 'news',
  type: 'document',
  title: 'News',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    ...
    {
      name: 'author',
      title: 'Author',
      type: 'string',
    },
    ...
  ],
  preview: {
    select: {
      title: 'title',
      subtitle: 'author',
    }
  }
}

这完全符合我在 Studio 中的要求。预览窗格中的部分显示title文档的标题,subtitle部分显示作者的姓名。

但是,如果我尝试author通过 using修改输出prepare,则它不再起作用。例如,看一下同一文档的以下变体:

export default {
  name: 'news',
  type: 'document',
  title: 'News',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    ...
    {
      name: 'author',
      title: 'Author',
      type: 'string',
    },
    ...
  ],
  preview: {
    select: {
      title: 'title',
      author: 'author',
    }
  },
  prepare(selection) {
    const { author } = selection
    return {
      ...selection,
      subtitle: author && `${author} is the author`
    }
  }
}

预览字段已呈现,但该部分title中没有显示任何内容。subtitle但是,据我了解 - 这应该有效。我想知道为什么不。

有任何想法吗?

4

1 回答 1

3

prepare实际上是在预览中调用的函数。您将其作为根对象的单独字段。像这样移动准备内部preview

preview: {
  select: {
    title: 'title',
    author: 'author'
  },
  prepare(selection) {
    const { author } = selection
    return {
      ...selection,
      subtitle: author && `${author} is the author`
    }
  }
}
于 2019-08-05T17:13:55.703 回答