Giter VIP home page Giter VIP logo

nuxt-class-component's Introduction

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):

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
  • Using tsconfig.json:
{
  "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

  // 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, ' + this.propMessage

  // lifecycle hooks
  created () {
    this.stateFoo // -> store.state.foo
    this.stateBar // -> store.state.bar
    this.getterFoo // -> store.getters.foo
    this.actionFoo({ value: true }) // -> store.dispatch('foo', { value: true })
    this.mutationFoo({ value: true }) // -> store.commit('foo', { value: true })
    this.moduleGetterFoo // -> store.getters['path/to/module/foo']
  }

  mounted () {
    this.greet()
  }

  fetch () {
    // fetch data
  }

  async asyncData () {
    // async fetching data
  }

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

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

License

MIT License - Copyright (c) Nuxt Community

nuxt-class-component's People

Contributors

breakingrobot avatar atinux avatar clarkdo avatar zl810881283 avatar johnlindquist avatar alexjoverm avatar pawel-schmidt avatar salihsagdilek avatar

Watchers

nikolasmelui avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.