hulai-website/composables/useSectionTitles.js

34 行
964 B
JavaScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

/**
* 栏目标题CommonSectionTitle统一读取器
*
* 用法:
* const st = await useSectionTitles()
* <CommonSectionTitle
* :title="st('summerCamp.principles', '行程设计四大原则')"
* :subtitle="st('summerCamp.principles', '每一处安排都经过精心考量', 'subtitle')"
* />
*
* st(path, fallback, field = 'title') 三个参数都是字符串。
* field 可选 'title' | 'subtitle' | 'label'。
*/
export async function useSectionTitles() {
const { data } = await usePublicApi('section-titles')
return (path, fallback = '', field = 'title') => {
const parts = (path || '').split('.')
let cursor = data.value
for (const p of parts) {
if (cursor && typeof cursor === 'object' && p in cursor) {
cursor = cursor[p]
} else {
return fallback
}
}
if (cursor && typeof cursor === 'object') {
return cursor[field] || fallback
}
return fallback
}
}