Nuxt Class Component
ES and Typescript Class Components Decorators for Nuxt.js extending vue-class-component
Installation
npm install --save nuxt-class-component # or yarn add nuxt-class-component
Babel Instructions
npm install --save-dev babel-plugin-transform-decorators-legacy babel-plugin-transform-class-properties # or yarn add --dev babel-plugin-transform-decorators-legacy babel-plugin-transform-class-properties
Then you can configure Babel plugins on nuxt.config.js
- Plugin order is important (see here):
moduleexports = build: babel: plugins: 'transform-decorators-legacy' 'transform-class-properties'
Typescript Instructions
You will need to enable [experimentalDecorators
] on your Typescript compiler.(http://www.typescriptlang.org/docs/handbook/decorators.html).
- Using Typescript Compiler argument
--experimentalDecorators
like this:
tsc --experimentalDecorators
- Using
tsconfig.json
:
Usage
See vue-class-component, vuex-class documentation.
You can also see the official TypeScript example of Nuxt.js.
Example
const Module = @ @ stateFoo @ stateBar @ getterFoo @ actionFoo @ mutationFoo @Module moduleGetterFoo @Module moduleActionFoo // If the argument is omitted, use the property name // for each state/getter/action/mutation type @State foo @Getter bar @Action baz @Mutation qux // initial data msg = 123 // use prop values for initial data helloMsg = 'Hello, ' + thispropMessage // lifecycle hooks { thisstateFoo // -> store.state.foo thisstateBar // -> store.state.bar thisgetterFoo // -> store.getters.foo this // -> store.dispatch('foo', { value: true }) this // -> store.commit('foo', { value: true }) thismoduleGetterFoo // -> store.getters['path/to/module/foo'] } { this } { // fetch data } async { // async fetching data } // computed { return 'computed ' + thismsg } // method { }
License
MIT License - Copyright (c) Nuxt Community