Nuxt Class Component
ES and Typescript Class Components Decorators for Nuxt.js extending vue-class-component
Installation
npm install --save nuxt-class-component
yarn add nuxt-class-component
Babel Instructions
npm install --save-dev babel-plugin-transform-decorators-legacy babel-plugin-transform-class-properties
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):
module.exports = {
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
{
"compilerOptions": {
"experimentalDecorators": true,
...
}
}
Usage
See vue-class-component, vuex-class documentation.
You can also see the official TypeScript example of Nuxt.js.
Example
import Vue from 'vue'
import Component from 'nuxt-class-component'
import {
State,
Getter,
Action,
Mutation,
namespace
} from 'vuex-class'
const Module = namespace('path/to/module')
@Component({
props: {
propMessage: String
}
})
export class MyComp extends Vue {
@State('foo') stateFoo
@State(state => state.bar) stateBar
@Getter('foo') getterFoo
@Action('foo') actionFoo
@Mutation('foo') mutationFoo
@Module.Getter('foo') moduleGetterFoo
@Module.Action('foo') moduleActionFoo
@State foo
@Getter bar
@Action baz
@Mutation qux
msg = 123
helloMsg = 'Hello, ' + this.propMessage
created () {
this.stateFoo
this.stateBar
this.getterFoo
this.actionFoo({ value: true })
this.mutationFoo({ value: true })
this.moduleGetterFoo
}
mounted () {
this.greet()
}
fetch () {
}
async asyncData () {
}
get computedMsg () {
return 'computed ' + this.msg
}
greet () {
alert('greeting: ' + this.msg)
}
}
License
MIT License - Copyright (c) Nuxt Community