JSPM

  • Created
  • Published
  • Downloads 317271
  • Score
    100M100P100Q168016F
  • License MIT

ES201X/TypeScript class decorator for Vue components

Package Exports

  • vue-class-component

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-class-component) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

vue-class-component

ECMAScript / TypeScript decorator for class-style Vue components.

Usage

Required: ECMAScript stage 1 decorators. If you use Babel, babel-plugin-transform-decorators-legacy is needed. If you use TypeScript, enable --experimentalDecorators flag.

Note:

  1. methods can be declared directly as class member methods.

  2. Computed properties can be declared as class property accessors.

  3. data, render and all Vue lifecycle hooks can be directly declared as class member methods as well, but you cannot invoke them on the instance itself. When declaring custom methods, you should avoid these reserved names.

  4. For all other options, pass them to the decorator function.

import Component from 'vue-class-component'

@Component({
  props: {
    propMessage: String
  },
  template: `
    <div>
      <input v-model="msg">
      <p>prop: {{propMessage}}</p>
      <p>msg: {{msg}}</p>
      <p>computed msg: {{computedMsg}}</p>
      <button @click="greet">Greet</button>
    </div>
  `
})
class App {
  // return initial data
  data () {
    return {
      msg: 123
    }
  }

  // lifecycle hook
  mounted () {
    this.greet()
  }

  // computed
  get computedMsg () {
    return 'computed ' + this.msg
  }

  // method
  greet () {
    alert('greeting: ' + this.msg)
  }
}

You may also want to check out the @prop and @watch decorators provided by vue-property-decorators.

Build the Example

$ npm install && npm run example

License

MIT