在Vue JS中,从vue实例内的方法调用过滤器 [英] In Vue JS, call a filter from a method inside the vue instance

查看:1268
本文介绍了在Vue JS中,从vue实例内的方法调用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像这样的Vue实例:

Say I have a Vue instance like so:

new Vue({
    el: '#app',

    data: {
        word: 'foo',
    },

    filters: {
       capitalize: function(text) {
           return text.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
       }
    },

    methods: {
        sendData: function() {
            var payload = this.$filters.capitalize(this.word); // how?
        }
    }
}

我可以轻松地在模板中使用过滤器:

I can easily use the filter in a template like so:

<span>The word is {{ word | capitalize }}</span>

但是如何在实例方法或计算属性中使用此过滤器?
(显然这个例子很简单,我的实际过滤器更复杂。)

But how can I use this filter from within an instance method or computed property? (Obviously this example is trivial and my actual filters are more complex).

推荐答案

this.$options.filters.capitalize(this.word);

参见 http://vuejs.org/api/#vm-options

这篇关于在Vue JS中,从vue实例内的方法调用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆