Search code examples
typescriptvue.jsvuejs2vue-componentvue-meta

How to access class members from component properties?


I need to create a mixin to set the header and meta data. For this i found vue-meta, which works great. But i'm not yet familiar with typscript and class based components.

how is the class member pageTitle reachable within component properties metaInfo()?

sample code:

import Vue from 'vue';
import { Component } from 'vue-property-decorator';

@Component({
  metaInfo() {
    return {
      title: pageTitle, // here I would like to access class member
    };
  },
})
export default class headerMixin extends Vue {
  pageTitle: string = 'Page Title'; // definition of class member
}


Solution

  • Considering that metaInfo function receives component instance as context, it can be done like:

    @Component({
      metaInfo(this: headerMixin) {
        return {
          title: this.pageTitle
        };
      },
    })
    export default class headerMixin extends Vue {
      pageTitle: string = 'Page Title';
    }