0
votes

I am using Nuxt 2.8.1 with typescript 3.4. When I create a component, and define "validate" for that page, I can't access es6 properties defined on the class:

@Component
class Foo extends Vue {
  get foo (): boolean { return true }

  validate () {
    return this.foo // returns undefined?
  }
}

[NB: Validate is not passed a Vue instance. At the very least, tooling should error if this for validate is not actually a Foo instance.]

How should I access foo?

UPDATE Ok ... (thanks @Mohan for question) the real problem here is that (being a nuxt/vue newbie), I was defining a vue file in pages, and thought that all vue files are components. However, a page is not a component. (I guess @Page decorator would be nice to support class-based pages.) But the moral of the story is that pages don't have class-based support, and thus isn't weird that validate() is not called with the "component instance".

1
this context is dependent on how the method is invoked. Is it you who invoke validate? If not - how exactly that is called? What are the requirements for that method set by a caller? - zerkms

1 Answers

0
votes

I'm not 100% sure if this is what you are asking, but it seems that you are using the get keyword incorrectly. I attached a small example of how I would generally use the get and set keywords. I think this example should be enough to guide you.

class Person{
    private _name:string;
    private _occupation:string;
    constructor(name:string, occupation:string){
        this._name = name;
        this._occupation = occupation
    }
    get occupation(){
        return this._occupation;
    }
    set occupation(occup:string){
        // do some grammar and spelling checking here too???

        this._occupation = occup;
    }
}
let Obama = new Person('Obama', 'President');
console.log(Obama.occupation);
// console.log(Obama._name) error
Obama.occupation = 'Chilling';
console.log(Obama.occupation);