mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-18 08:30:33 +03:00
36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import { isFunction } from '@vue/shared'
|
|
import { App, ComponentOptionsMixin, computed, getCurrentInstance } from 'vue'
|
|
import { ComponentOptionsMetaInfo } from './types/options'
|
|
import { useMeta } from './useApi'
|
|
|
|
export type PluginOptions = {
|
|
keyName: string
|
|
}
|
|
|
|
export const defaultOptions: PluginOptions = {
|
|
keyName: 'metaInfo'
|
|
}
|
|
|
|
type CreateMixin = (options: PluginOptions) => ComponentOptionsMixin
|
|
export const createMixin: CreateMixin = options => ({
|
|
created () {
|
|
const instance = getCurrentInstance()
|
|
if (!instance?.type || !(options.keyName in instance.type)) {
|
|
return
|
|
}
|
|
|
|
const metaInfo = (instance.type as any)[options.keyName] as ComponentOptionsMetaInfo
|
|
if (isFunction(metaInfo)) {
|
|
const computedMetaInfo = computed(metaInfo)
|
|
useMeta(computedMetaInfo)
|
|
} else {
|
|
useMeta(metaInfo)
|
|
}
|
|
}
|
|
})
|
|
|
|
export const install = (app: App, _options: Partial<PluginOptions> = {}) => {
|
|
const options = Object.assign({}, defaultOptions, _options)
|
|
app.mixin(createMixin(options))
|
|
}
|