Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nuxt-property-decorator

nuxt-community95kMIT2.9.1TypeScript support: included

Property decorators for Nuxt

vue, typescript, nuxt, decorator

readme

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

It works out of the box with Nuxt JS.

Nuxt TS Instructions

It works out of the box with Nuxt TS.

Decorators and helpers

There are following decorators:

Other exports

  • namespace
  • mixins
  • Vue

Hooks

Vue Router hooks

  • beforeRouteEnter
  • beforeRouteUpdate
  • beforeRouteLeave

Nuxt hooks

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

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.

See also:

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.9.1 (2021-01-15)

2.8.8 (2020-09-23)

2.8.7 (2020-09-23)

2.8.6 (2020-09-23)

2.8.5 (2020-09-23)

2.8.3 (2020-09-18)

2.8.2 (2020-09-18)

2.7.2 (2020-04-21)

2.7.1 (2020-04-21)

2.7.0 (2020-04-21)

Features

  • add other formats to package.json (dbdc488)

2.5.1 (2020-02-17)

Bug Fixes

2.5.0 (2019-10-18)

Features

  • add meta hook and update documentation (6562108)

2.4.1 (2019-10-17)

2.4.0 (2019-09-02)

Features

  • deps: updates vue-property-decorator (e9ee54e)
    • Added @PropSync decorator
    • Added @ProvideReactive and @InjectReactive decorator
    • Added @Ref decorator

2.3.0 (2019-06-02)

Features

  • deps: updates vue-property-decorator (6fd8195)

2.1.3 (2019-02-26)

2.0.1 (2019-02-08)

Library is fully inline with Vue-class-component

Breaking changes

There is only one breaking change and that is renaming mixins to Mixins to keep inline with vue-property-decorator

1.2.0 (2018-04-12)

Features

  • add build directly into the versionning (d235bec)

1.1.8 (2018-04-12)

Bug Fixes

  • postinstall: build after postinstall (46f8990)

1.1.7 (2018-04-12)

Bug Fixes

1.1.6 (2018-04-12)

Bug Fixes

  • build: automatically build umd (b7df4c4)

1.1.5 (2018-04-12)

Bug Fixes

  • build: build typescript as js module (a45ab44)