zhiqingstudy

Be a young person with knowledge and content

The filter defined under the filter node is called "private filter" because it can only be used in the el area controlled by the current vm instance.

If you want to share a filter among multiple vue instances, you can define a global filter in the following format:

    <div id="app1">
        <p>这是{{msg | toUpper}}</p>
    </div>
    <div id="app2">
        <p>这是{{msg | capi}}</p>
    </div>
    <script>
        // 在vue实例之外定义Vue.filter()全局过滤器
        Vue.filter('capi',(str)=>{
            const first=str.charAt(0).toUpperCase()
            const others=str.slice(1)
            return first+others
        })
        const vm1=new Vue({
            el:'#app1',
            data:{
                msg:'hello vue.js'
            },
            // 定义私有过滤器
            filters:{
                toUpper(msg){
                    return msg.toUpperCase()
                }
            }
        })
        const vm2=new Vue({
            el:'#app2',
            data:{
                msg:'vue.js'
            }
        })
    </script>

Precautions for filter:

1. To be defined under the filters node, it is essentially a function.

2. There must be a return in the filter function.

3. In the filter parameter, you can get the value to be processed before the &#34;pipe character&#34;

4. If the global filter name is the same as the private filter name, then the &#34;private filter&#34; is called according to the proximity principle

comment
head sculpture
Code:
Related

The art of interpersonal communication and communication

A code of conduct that regulates and regulates interpersonal relationships

The importance of interpersonal relationships




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号