|
10 | 10 |
|
11 | 11 | ```bash
|
12 | 12 | npm install --save nuxt-class-component
|
| 13 | + |
| 14 | +# or |
| 15 | + |
| 16 | +yarn add nuxt-class-component |
| 17 | +``` |
| 18 | + |
| 19 | +If use `babel`, install babel plugin: |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install --save-dev babel-plugin-syntax-flow babel-plugin-transform-flow-strip-types |
| 23 | + |
| 24 | +# or |
| 25 | + |
| 26 | +yarn add babel-plugin-syntax-flow babel-plugin-transform-flow-strip-types |
| 27 | +``` |
| 28 | + |
| 29 | +Config babel plugins, `nuxt.config.js`: |
| 30 | + |
| 31 | +```js |
| 32 | +module.exports = { |
| 33 | + build: { |
| 34 | + babel: { |
| 35 | + plugins: ['transform-decorators-legacy', 'transform-class-properties'] |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
13 | 40 | ```
|
14 | 41 |
|
15 | 42 | ## Usage
|
16 | 43 |
|
17 |
| -See [vue-class-component](https://github.com/vuejs/vue-class-component) documentation. |
| 44 | +See [vue-class-component](https://github.com/vuejs/vue-class-component), [vuex-class](https://github.com/vuejs/vuex) documentation. |
18 | 45 |
|
19 | 46 | You can also see the [official TypeScript example of Nuxt.js](https://github.com/nuxt/nuxt.js/tree/dev/examples/typescript).
|
20 | 47 |
|
| 48 | +### Example |
| 49 | + |
| 50 | +```js |
| 51 | +import Vue from 'vue' |
| 52 | +import Component from 'nuxt-class-component' |
| 53 | +import { |
| 54 | + State, |
| 55 | + Getter, |
| 56 | + Action, |
| 57 | + Mutation, |
| 58 | + namespace |
| 59 | +} from 'vuex-class' |
| 60 | + |
| 61 | +const ModuleGetter = namespace('path/to/module', Getter) |
| 62 | + |
| 63 | +@Component({ |
| 64 | + props: { |
| 65 | + propMessage: String |
| 66 | + } |
| 67 | +}) |
| 68 | +export class MyComp extends Vue { |
| 69 | + @State('foo') stateFoo |
| 70 | + @State(state => state.bar) stateBar |
| 71 | + @Getter('foo') getterFoo |
| 72 | + @Action('foo') actionFoo |
| 73 | + @Mutation('foo') mutationFoo |
| 74 | + @ModuleGetter('foo') moduleGetterFoo |
| 75 | + |
| 76 | + // If the argument is omitted, use the property name |
| 77 | + // for each state/getter/action/mutation type |
| 78 | + @State foo |
| 79 | + @Getter bar |
| 80 | + @Action baz |
| 81 | + @Mutation qux |
| 82 | + |
| 83 | + // initial data |
| 84 | + msg = 123 |
| 85 | + |
| 86 | + // use prop values for initial data |
| 87 | + helloMsg = 'Hello, ' + this.propMessage |
| 88 | + |
| 89 | + // lifecycle hooks |
| 90 | + created () { |
| 91 | + this.stateFoo // -> store.state.foo |
| 92 | + this.stateBar // -> store.state.bar |
| 93 | + this.getterFoo // -> store.getters.foo |
| 94 | + this.actionFoo({ value: true }) // -> store.dispatch('foo', { value: true }) |
| 95 | + this.mutationFoo({ value: true }) // -> store.commit('foo', { value: true }) |
| 96 | + this.moduleGetterFoo // -> store.getters['path/to/module/foo'] |
| 97 | + } |
| 98 | + |
| 99 | + mounted () { |
| 100 | + this.greet() |
| 101 | + } |
| 102 | + |
| 103 | + fetch () { |
| 104 | + // fetch data |
| 105 | + } |
| 106 | + |
| 107 | + async asyncData () { |
| 108 | + // async fetching data |
| 109 | + } |
| 110 | + |
| 111 | + // computed |
| 112 | + get computedMsg () { |
| 113 | + return 'computed ' + this.msg |
| 114 | + } |
| 115 | + |
| 116 | + // method |
| 117 | + greet () { |
| 118 | + alert('greeting: ' + this.msg) |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
21 | 123 | ## License
|
22 | 124 |
|
23 | 125 | [MIT License](./LICENSE)
|
|
0 commit comments