简体   繁体   English

如何在 Vue 类组件中定义过滤器?

[英]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. Vue 类组件是一种相对较新的编写单文件组件的方式。 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://v2.vuejs.org/v2/guide/typescript.html#Class-Style-Vue-Components https://github.com/vuejs/vue-class-component https://v2.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, eg:然后尝试将过滤器编写为类方法,例如:

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

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

I get the following error:我收到以下错误:

在此处输入图像描述

What's the right way to add a filter in this new syntax?在这种新语法中添加过滤器的正确方法是什么?

In a class component, the key is this comment ( // All component options are allowed in here ) in the docs:在类组件中,关键是文档中的这条注释( // All component options are allowed in here ):

// 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 部分中,您应该能够添加一个带有过滤器功能的filters对象,如下所示

@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 :根据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).初始数据可以声明为类属性(如果使用 Babel,则需要 babel-plugin-transform-class-properties)。

  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. data、render 和所有 Vue 生命周期钩子也可以直接声明为类成员方法,但您不能在实例本身上调用它们。 When declaring custom methods, you should avoid these reserved names.声明自定义方法时,应避免使用这些保留名称。

  5. For all other options, pass them to the decorator function.对于所有其他选项,将它们传递给装饰器函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM