【发布时间】:2019-03-20 07:18:33
【问题描述】:
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 https://github.com/vuejs/vue-class-component
但是,这些都没有解释如何用这种语法编写过滤器。如果我在模板中尝试以下代码:
{{ output | stringify }}
然后尝试将过滤器编写为类方法,例如:
@Component
export default class HelloWorld extends Vue {
// ... other things
stringify(stuff: any) {
return JSON.stringify(stuff, null, 2);
}
}
我收到以下错误:
在这种新语法中添加过滤器的正确方法是什么?
【问题讨论】:
-
你能分享codesandbox.io中的例子吗?
标签: javascript typescript vue.js vuejs2