如果我有一个简单的过滤器,请说:
Vue.filter('foo', function (value) {
return value.replace(/foo/g, 'bar');
});
还有一个简单的组件:
Vue.component('example', {
props: {
msg: String,
},
});
在标记内:
<example inline-template :msg="My foo is full of foo drinks!">
{{ msg }}
</example>
我可以简单地应用过滤器:
<example inline-template :msg="My foo is full of foo drinks!">
{{ msg | foo }}
</example>
我可以轻松地在模板中应用过滤器,但是我想将该逻辑移回到组件中。
事实并非如此need作为一个过滤器,但基本上是一种为数据字段创建 getter 和 setter 的方法。
就像是:
Vue.component('example', {
props: {
msg: {
type: String,
getValue: function(value) {
return value.replace(/foo/g, 'bar');
},
}
},
});