Vuejs 2 : How to make data lowercase

放肆的年华 提交于 2020-05-28 14:54:17

问题


I trying to make some data to lowercase (alway lowercase)

I making and search input like :

<template id="search">
    <div>
        <input type="text" v-model="search">
        <li v-show="'hello'.includes(search) && search !== ''">Hello</li>
    </div>
</template>

Vuejs : (component)

Vue.component('search', {
    template : '#search',
    data: function(){return{
        search : '',
    }}
});

I tried watch, But I dont want input showing lowercase when typing

watch: {
    'search' : function(v) {
        this.search = v.toLowerCase().trim();
    }
}

Demo : https://jsfiddle.net/rgr2vnjp/


And I dont want to add .toLowerCase() on search list v-show like :

<li v-show="'hello'.includes(search.toLowerCase()) && search !== ''">Hello</li>

Any trick?, I searched and many tell just use filter but not exits on Vuejs 2

Playground : https://jsfiddle.net/zufo5mhq/ (Try to type H)

PS: Good / better code I would like to know also, Thanks


回答1:


In Vue.js 2.0, computed properties can be used to replace filters:

computed: {
  searchInLowerCase() {
    return this.search.toLowerCase().trim();
  }
}

And now you can simply use searchInLowerCase in your template:

<li v-show="'hello'.includes(searchInLowerCase) && search !== ''">Hello</li>



回答2:


You could even do this

{{tag.title.toLowerCase().trim()}}


来源:https://stackoverflow.com/questions/43627497/vuejs-2-how-to-make-data-lowercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!