Skip to content

Why is 'vue-property-decorator' library not a part of vue-class-component? #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tonypee opened this issue Dec 24, 2016 · 7 comments
Closed

Comments

@tonypee
Copy link

tonypee commented Dec 24, 2016

To write vue components using classes, we need to be able to do things such as 'watch' values. It makes sense to me that this should be a part of the same repo?

Looking at vue-property-decorator, since it can export Componenti guess we have this functionality already, just under a different (less appropriate library name)

import { Component, prop, watch } from 'vue-property-decorator';

should be

import { Component, prop, watch } from 'vue-class-component';

or

import { prop, watch }, Component from 'vue-class-component';

@ktsn
Copy link
Member

ktsn commented Dec 24, 2016

Because when we use prop decorator with Babel, it causes some problems while it really works well with TypeScript.
As vue-class-component also supports Babel users, unfortunately we cannot include it in this library.

@ktsn ktsn closed this as completed Dec 24, 2016
@tonypee
Copy link
Author

tonypee commented Dec 24, 2016 via email

@ktsn
Copy link
Member

ktsn commented Dec 24, 2016

The issue is caused by the difference of behavior between babel-transform-decorators-legacy and TypeScript's decorators.

For example in the following case:

@Component
class MyComp extends Vue {
  foo = 'foo + ' + this.bar
  @prop(String) bar
  baz = 'baz + ' + this.bar 
}

const c = new MyComp({
  propsData: { bar: 'bar' }
})

In TypeScript, this works as expected foo === 'foo + bar', bar === 'bar', baz === 'baz + bar'.
In Babel, however, foo === 'foo + bar', bar === 'bar', baz === 'baz + undefined'.

This is because Babel decorators will initialize a decorated property as undefined even if it does not have an initializer while TypeScript decorators won't initialize it.

@lbssousa
Copy link

lbssousa commented Jun 5, 2017

Any chance this scenario could change with improved decorators support in upcoming Babel 7?

@davidm-public
Copy link

Just curious if this results in odd behaviour with Storybook?

@JessicaSachs
Copy link

@davidm-public Are you even able to get storybook working with vue-class-component? I'm struggling with it.

@b0g3r
Copy link

b0g3r commented Feb 17, 2019

FYI: @JessicaSachs @davidm-public
I have the same trouble with vue-class-component + vue + typescript. If my little research is right, __docs is undefined, because component options have strict typing and don't allow any new property without definition.

I just added the file, as in the documentation, and voila.
doc-property.d.ts

import Vue from 'vue'
declare module 'vue/types/options' {
    interface ComponentOptions<V extends Vue> {
      __doc?: string
    }
}

stories/index.js

storiesOf('MyComponent', module)
  .addDecorator(withDocs(MyComponent.options.__docs))
  ...

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

No branches or pull requests

6 participants