【问题标题】:Preview a reference预览参考
【发布时间】:2018-08-09 16:02:01
【问题描述】:

我想在工作室中预览参考名称 我有图标类型,例如标题为“facebook”的图标类型

export default {
name: 'icon',
title: 'Icon',
type: 'document',
fields: [
    {
        name: 'name',
        title: 'Name',
        type: 'string'
    },
]

}

我在其他地方的菜单中引用了这个

{
        name: 'icon',
        title: 'Icon',
        type: 'reference',
        to: [{ type: 'icon' }]
    },

然后尝试像这样预览

preview: {
    select: {
        title: 'icon',
    },
    prepare(selection) {
        const { title } = selection;

        return {
            title: title.name,
        }
    }
}

但我的选择返回参考对象,_ref 等不是对象本身。有没有办法预览这个参考?

【问题讨论】:

    标签: sanity


    【解决方案1】:

    您可以到您想在预览中使用的引用上的属性,如下所示:

    preview: {
        select: {
            title: 'icon.name',
        },
        prepare(selection) {
            const { title } = selection;
    
            return {
                title: title.name,
            }
        }
    }
    

    旁注:由于 prepare 函数现在只是传递其输入,因此您可以完全删除它。这样就足够了:

    preview: {
        select: {
            title: 'icon.name'
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2020-05-28
      • 2021-03-13
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多