Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Computed attributes #85

Closed
brunosiqueira opened this issue Mar 1, 2018 · 8 comments
Closed

Computed attributes #85

brunosiqueira opened this issue Mar 1, 2018 · 8 comments

Comments

@brunosiqueira
Copy link

How can I use computed attributes with decorators?
Thanks!

@ninjascant
Copy link

As mentioned in this tutorial, computed properties are implemented in vue-property-decorator as ES6 getters:

@Component
export default class App extends Vue {
  get myComputedProp() {
    return Math.random()
  }
}

Which is equal to this plain Vue syntax:

export default {
  computed: {
    myComputedProp() {
      return Math.random()
    }
  }
}

@barakbd-bluevine
Copy link

What about setters?
I could not find an example of that

@kaorun343
Copy link
Owner

@barakbd-bluevine

Hi.

@Component
export default class App extends Vue {
  set myComputedProp(value: any) {
    console.log(value)
  }
}

@awacode21
Copy link

What about two-way computed properties (with get and set) in TypeScript. I absolutely do not find any example on this

@GeniaT
Copy link

GeniaT commented Mar 5, 2019

Would be great to add computed attributes examples to the documentation.

@kaorun343
Copy link
Owner

kaorun343 commented Mar 5, 2019

Converting from getters / setters into computed properties is provided by vue-class-component. Please ask to the repository.

@Component
class YourComponent extends Vue {
  firstname = ''
  lastname = ''

  get value() {
    return `${this.firstname} ${this.lastname}`
  }

  set value(fullname: string) {
    const [firstname, lastname] = fullname.split(' ')
    this.firstname = firstname
    this.lastname = lastname
  }
}

@oceanlvr
Copy link

How to use async / await in the getters / setters?

@kaorun343
Copy link
Owner

It'll be supported if the Vue itself does.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants