Vue类组件是编写单文件组件的一种相对较新的方式.看起来是这样的:
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)
}
}
以下是对它的一些参考:
https://vuejs.org/v2/guide/typescript.html#Class-Style-Vue-Components
然而,这些都没有解释如何用这种语法编写过滤器.如果我在模板中try 以下代码:
{{ output | stringify }}
然后try 将筛选器作为类方法编写,例如:
@Component
export default class HelloWorld extends Vue {
// ... other things
stringify(stuff: any) {
return JSON.stringify(stuff, null, 2);
}
}
我得到以下错误:
用这种新语法添加过滤器的正确方法是什么?