question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Vue v3] Changes for @Component decorator and Vue base class

See original GitHub issue

Summary

  • @Component will be renamed to @Options.
  • @Options is optional if you don’t declare any options with it.
  • Vue constructor is provided from vue-class-component package.
  • Component.registerHooks will move to Vue.registerHooks

Example:

<template>
  <div>{{ count }}</div>
  <button @click="increment">+1</button>
</template>

<script>
import { Vue, Options } from 'vue-class-component'

// Component definition
@Options({
  // Define component options
  watch: {
    count: value => {
      console.log(value)
    }
  }
})
export default class Counter extends Vue {
  // The behavior in class is the same as the current
  count = 0

  increment() {
    this.count++
  }
}
</script>
// Adding lifecycle hooks
import { Vue } from 'vue-class-component'

Vue.registerHooks([
  'beforeRouteEnter',
  'beforeRouteLeave',
  'beforeRouteUpdate'
])

Details

As Vue v3 no longer provides base Vue constructor, Vue Class Component will provide it instead. Fortunately, we can add class component specific features in the base class as we define it in Vue Class Component package.

One of the benefits with the new Vue constructor is we can make @Component decorator optional. There were non-trivial number of confusions regarding missing @Component decorator in super class / mixins (e.g. #180). Making decorator optional would solve this problem.

Also renaming @Component with @Options would make more sense as it is for adding component options to your class components rather than making a class component.

Since @Options is optional, having registerHooks on the decorator will not work. So we will move the method under static field of Vue constructor.

Alternative approach

Declaring component options as static properties

We could do this to define class component options:

export default class Counter extends Vue {
  // All component options are static properties
  static watch = {
    count: value => {
      console.log(value)
    }
  }

  count = 0

  increment() {
    this.count++
  }
}

But it has a drawback: we cannot define static properties / methods not for component options both on userland and library side. e.g. we cannot define static method like registerHooks on Vue.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:138
  • Comments:58 (4 by maintainers)

github_iconTop GitHub Comments

123reactions
theoephraimcommented, Dec 15, 2020

Maybe it’s time that vue-class-component and vue-property-decorator merge into a single tool? It seems like it would be easier to design a better and more wholistic solution this way.

53reactions
iklemmcommented, Nov 1, 2020

Seems that this new API does not match with documentation https://class-component.vuejs.org which is very confusing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue Class Component: Overview
Vue Class Component is a library that lets you make your Vue components in class-style syntax. For example, below is a simple counter...
Read more >
What is the meaning of @Options in Vue 3 with class ...
I have a project using Vue 3 with class-component and typescript , and I found syntax @Option in the App.vue file
Read more >
Define properties with Vue Property Decorator and TypeScript
Define properties like data, methods, and watchers directly on the class in Vue components with Vue Property Decorator and TypeScript.
Read more >
How To Write Class-Based Components with Vue.js and ...
A class component is a TypeScript class that extends the Vue object. In single-file components, make sure you set the <script> language to...
Read more >
Vue 3 composition API with Typescript class components
Vue 3 is great, specially the composition API is an awesome new feature. Migrating Vue 2 class based components to Vue 3 is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found