Vue过滤器 filters

Posted antao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vue过滤器 filters相关的知识,希望对你有一定的参考价值。

局部过滤器:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>filters</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <div id="app">
            <input type="text" v-model="test" placeholder="请输入"/>
            <div>{{test|filter1|filter2}}</div>
        </div>
    </body>
</html>
<script type="text/javascript" charset="utf-8">
    var app = new Vue({
        el: "#app",
        data: {
            test: ‘‘
        },
        filters: {
            filter1: function(val) {
                return val.toUpperCase();
            },
            filter2: function(val) {
                var reg = /d/g;
                return val.replace(reg, *);
            }
        }
    });
</script>

 

全局过滤器:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>filters</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <div id="app">
            <input type="text" v-model="test" placeholder="请输入" />
            <div>{{test|filter1|filter2}}</div>
        </div>
    </body>
</html>
<script type="text/javascript" charset="utf-8">
    Vue.filter(filter1, function(val) {
        return val.toUpperCase();
    });
    
    Vue.filter(filter2, function(val) {
        var reg = /d/g;
        return val.replace(reg, *);
    });
    
    var app = new Vue({
        el: "#app",
        data: {
            test: ‘‘
        },
    });
</script>

 

以上是关于Vue过滤器 filters的主要内容,如果未能解决你的问题,请参考以下文章

vue实例中的filters属性

Vue3中删除了过滤器filter功能-如何使用computed和methods实现filter功能

js简洁代码片段

Vue2.0 注册过滤器Vue.filter

Vue-Methods中使用Filter

vue-过滤器(filter)