34 行
964 B
JavaScript
34 行
964 B
JavaScript
/**
|
||
* 栏目标题(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
|
||
}
|
||
}
|