Giter Site home page Giter Site logo

yoshithechinchilla / nuxt-property-decorator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nuxt-community/nuxt-property-decorator

0.0 1.0 0.0 577 KB

Property decorators for Nuxt (base on vue-property-decorator)

Home Page: https://github.com/kaorun343/vue-property-decorator

License: MIT License

TypeScript 47.35% JavaScript 52.65%

nuxt-property-decorator's Introduction

Nuxt Property Decorator


Handy ES / TypeScript decorators for class-style Vue components in Nuxt (based on Vue class component) and Property decorators for Vue (bases on Vue Property Decorator) and Vuex (based on Vuex Class)

This library fully depends on vue-class-component.

License

MIT License

Install

Installation is very easy

npm i -S nuxt-property-decorator

or

yarn add nuxt-property-decorator

Nuxt JS Instructions

Currently decorators need the following two Babel plugins to work @babel/plugin-proposal-decorators,"@babel/plugin-proposal-class-properties. Latest Nuxt already adds them for us, the only thing we need is to add loose parameter to @nuxt/babel-preset-app. Just add this to nuxt-config

. build: { babel: { presets({ isServer }) { return [ [ "@nuxt/babel-preset-app", { loose: true } ] ] } } } (as there is an issue with @nuxt/babel-preset-app, meanwhile just use the following config)

  build: {
    babel: {
      plugins: [
        ["@babel/plugin-proposal-decorators", { legacy: true }],
        ["@babel/plugin-proposal-class-properties", { loose: true }]
      ]
    }
  }

Nuxt TS Instructions

It works out of the box with Nuxt TS.

Decorators and helpers

There are following decorators:

  • exported from vue-class-component
    • @Component
  • exported from vue-property-decorator
    • @Emit
    • @Inject
    • @Model
    • @Prop
    • @Provide
    • @Watch
  • exported from vuex-class
    • @State
    • @Getter
    • @Action
    • @Mutation

Other exports

  • namespace
  • mixins
  • Vue

Hooks

Vue Router hooks

  • beforeRouteEnter
  • beforeRouteUpdate
  • beforeRouteLeave

Nuxt hooks

  • asyncData
  • fetch
  • head
  • key
  • layout
  • loading
  • middleware
  • scrollToTop
  • transition
  • validate
  • watchQuery

Vue-class Hooks

  • data
  • beforeCreate
  • created
  • beforeMount
  • mounted
  • beforeDestroy
  • destroyed
  • beforeUpdate
  • updated
  • activated
  • deactivated
  • render
  • errorCaptured
  • serverPrefetch

Usage

import { Component, Inject, Model, Prop, Provide, Vue, Watch } from 'nuxt-property-decorator'

const s = Symbol('baz')

@Component({
  components: { comp }
})
export class MyComponent extends Vue {

  @Inject() foo!: string
  @Inject('bar') bar!: string
  @Inject(s) baz!: string

  @Model('change') checked!: boolean

  @Prop()
  propA!: number

  @Prop({ default: 'default value' })
  propB!: string

  @Prop([String, Boolean])
  propC!: string | boolean

  @Prop({ type: null })
  propD!: any

  @Provide() foo = 'foo'
  @Provide('bar') baz = 'bar'
 
  @Watch('child')
  onChildChanged(val: string, oldVal: string) { }

  @Watch('person', { immediate: true, deep: true })
  onPersonChanged(val: Person, oldVal: Person) { }
  
  beforeRouteLeave (to, from, next) {
    // called when the route that renders this component is about to
    // be navigated away from.
    // has access to `this` component instance.
  }
}

is equivalent to

const s = Symbol('baz')

export const MyComponent = Vue.extend({
  name: 'MyComponent',
  components: { comp },
  inject: {
    foo: 'foo',
    bar: 'bar',
    [s]: s
  },
  model: {
    prop: 'checked',
    event: 'change'
  },
  props: {
    checked: Boolean,
    propA: Number,
    propB: {
      type: String,
      default: 'default value'
    },
    propC: [String, Boolean],
    propD: { type: null }
  },
  data () {
    return {
      foo: 'foo',
      baz: 'bar'
    }
  },
  provide () {
    return {
      foo: this.foo,
      bar: this.baz
    }
  },
  methods: {
    onChildChanged(val, oldVal) { },
    onPersonChanged(val, oldVal) { }
  },
  beforeRouteLeave (to, from, next) {
    // called when the route that renders this component is about to
    // be navigated away from.
    // has access to `this` component instance.
  },
  watch: {
    'child': {
      handler: 'onChildChanged',
      immediate: false,
      deep: false
    },
    'person': {
      handler: 'onPersonChanged',
      immediate: true,
      deep: true
    }
  }
})

As you can see at propA and propB, the type can be inferred automatically when it's a simple type. For more complex types like enums you do need to specify it specifically. Also this library needs to have emitDecoratorMetadata set to true for this to work.

Useful links

See also:

nuxt-property-decorator's People

Contributors

breakingrobot avatar davidbernal avatar husayt avatar inouetakuya avatar matjaz avatar nysos3 avatar sobolevn avatar yoshithechinchilla avatar

Watchers

 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.