nuxt-property-decorator icon indicating copy to clipboard operation
nuxt-property-decorator copied to clipboard

Cannot access this inside head

Open komninoschatzipapas opened this issue 4 years ago • 2 comments

I have a component that calculates the page title on asyncData. It looks like this:

import { Component, Vue } from 'nuxt-property-decorator';

@Comonent({
  head() { return { title: this.title } },
  asyncData(ctx) {
    // Calculate title dynamically
    return { title: 'abc' }
  } 
})
export default Comp extends Vue {
  title!: string;
}

But I get an error while trying to access this inside head:

Property 'title' does not exist on type 'Vue'. Vetur(2339)

Is it possible to achieve this functionality? If yes, how exactly?

komninoschatzipapas avatar Feb 17 '21 18:02 komninoschatzipapas

Why not use the head-hook ?

amjmhs avatar Mar 10 '21 06:03 amjmhs

@komninoschat You can use it by specifying this as a parameter for the head. Using your example it will look this way:

import { Component, Vue } from 'nuxt-property-decorator';

@Comonent({
  head(this: Comp) { return { title: this.title } },
  asyncData(ctx) {
    // Calculate title dynamically
    return { title: 'abc' }
  } 
})
export default Comp extends Vue {
  title!: string;
}

maxdzin avatar Dec 01 '21 08:12 maxdzin