From 1f22adbce83474e15d03d708090ca36bad4752c8 Mon Sep 17 00:00:00 2001 From: pimlie Date: Sun, 1 Aug 2021 20:30:32 +0200 Subject: [PATCH] fix #699: bind component instance to computed metaInfo --- examples/vue-router/Options.ts | 18 ++++++++++++++---- src/plugin.ts | 2 +- src/types/options.ts | 5 ++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/examples/vue-router/Options.ts b/examples/vue-router/Options.ts index cff295f..f4d7e48 100644 --- a/examples/vue-router/Options.ts +++ b/examples/vue-router/Options.ts @@ -1,6 +1,16 @@ -export default { - metaInfo: { - title: 'Title from Options API' +import { defineComponent } from 'vue' + +export default defineComponent({ + data () { + return { + title: 'Data Title' + } + }, + metaInfo () { + const title = this.title + return { + title: title + ' from Options API' + } }, template: '
This component uses the Options API
' -} +}) diff --git a/src/plugin.ts b/src/plugin.ts index c402c3a..70cd624 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -21,7 +21,7 @@ export const createMixin: CreateMixin = options => ({ const metaInfo = (instance.type as any)[options.keyName] as ComponentOptionsMetaInfo if (isFunction(metaInfo)) { - const computedMetaInfo = computed(metaInfo) + const computedMetaInfo = computed(metaInfo.bind(this)) useMeta(computedMetaInfo) } else { useMeta(metaInfo) diff --git a/src/types/options.ts b/src/types/options.ts index 436260e..fe28227 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -1,3 +1,5 @@ +import { ComponentOptionsMixin } from 'vue' + export interface AttributeProperty { [key: string]: string | string[] } @@ -129,4 +131,5 @@ export interface ComponentMetaInfo { noscript?: NoScriptProperty[] } -export type ComponentOptionsMetaInfo = ComponentMetaInfo | (() => ComponentMetaInfo) +// TODO: how to infer the real component options in this? +export type ComponentOptionsMetaInfo = ComponentMetaInfo | ((this: ComponentOptionsMixin) => ComponentMetaInfo)