Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define a filter in a Vue class component?

The Vue class component is a relatively new way of writing single-file components. It looks like this:

import Vue from 'vue'
import Component from 'vue-class-component'

// The @Component decorator indicates the class is a Vue component
@Component({
  // All component options are allowed in here
  template: '<button @click="onClick">Click!</button>'
})
export default class MyComponent extends Vue {
  // Initial data can be declared as instance properties
  message: string = 'Hello!'

  // Component methods can be declared as instance methods
  onClick (): void {
    window.alert(this.message)
  }
}

Here are some references to it:

https://vuejs.org/v2/guide/typescript.html#Class-Style-Vue-Components https://github.com/vuejs/vue-class-component

However, none of those explain how to write filters in this syntax. If I try the following code in my template:

{{ output | stringify }}

And then try to write a filter as a class method, e.g.:

@Component
export default class HelloWorld extends Vue {
  // ... other things

  stringify(stuff: any) {
    return JSON.stringify(stuff, null, 2);
  }    
}

I get the following error:

enter image description here

What's the right way to add a filter in this new syntax?

like image 869
Andrew Mao Avatar asked Oct 15 '18 19:10

Andrew Mao


People also ask

How many ways are there to define a filter in VUE JS?

In Vue, we can define and implement the filter in two different ways which are explained below.

Where can Vue filters be applied?

Going off number 2, because VueJS filters are meant for text transformations, they can only be used in two places: mustache interpolations (the curly braces in your template) and in v-bind expressions.

How do I define a Vue component?

The quickest and easiest way to define a Vue component template is to add a template property to the component definition and assign a regular string containing your markup.


1 Answers

In a class component, the key is this comment (// All component options are allowed in here) in the docs:

// The @Component decorator indicates the class is a Vue component
@Component({
  // All component options are allowed in here
  template: '<button @click="onClick">Click!</button>'
})

This means that in the @Component section you should be able to add a filters object with your filter functions inside, like this

@Component({
  // other options
  filters: {
    capitalize: function (value) {
      if (!value) return ''
      value = value.toString()
      return value.charAt(0).toUpperCase() + value.slice(1)
    }
  }
})

According to doc in https://github.com/vuejs/vue-class-component:

Note:

  1. methods can be declared directly as class member methods.

  2. Computed properties can be declared as class property accessors.

  3. Initial data can be declared as class properties (babel-plugin-transform-class-properties is required if you use Babel).

  4. data, render and all Vue lifecycle hooks can be directly declared as class member methods as well, but you cannot invoke them on the instance itself. When declaring custom methods, you should avoid these reserved names.

  5. For all other options, pass them to the decorator function.

like image 99
tato Avatar answered Oct 06 '22 10:10

tato