简体   繁体   English

在Vue.JS方法中,传递的字符串trim()不起作用

[英]In Vue.JS method on passed string trim() doesn't work

So I am trying to remove white space from one of the returned object's property with .trim() function but it doesn't work. 因此,我试图使用.trim()函数从返回的对象的属性之一中删除空格,但它不起作用。

I already tried applying JS functions such as .replace(/\\s/g,'') but it is also not working 我已经尝试过应用.replace(/ \\ s / g,'')之类的JS函数,但它也无法正常工作

<script>
export default {
    name: 'navigation',
    props: ["navigation"],
    methods:{
        whiteSpace: function(a){
            var str = a;
            str.toLowerCase();
            str.trim();
            console.log(str);


            return str;
        }
    }
}
</script>

HTML HTML

<li>
  <router-link :to="{ path: whiteSpace(nav.linkTitle) }">
       {{nav.linkTitle}}
  </router-link>
</li>

It should return string without white space but it doesn't remove white space. 它应该返回不带空格的字符串,但不会删除空格。

The trim method does not modify the object, it returns a new string. trim方法不会修改对象,它会返回一个新字符串。

just return with return str.trim() 只需返回return str.trim()

This code: 这段代码:

str.toLowerCase();

returns a new value. 返回一个新值。 It doesn't modify the current value, so you should have done this: 它不会修改当前值,因此您应该这样做:

var str = a;
str = str.toLowerCase();
str = str.trim();
console.log(str);

but any ways, you could just return the trimmed value like so: 但无论如何,您都可以像这样返回修整后的值:

export default {
    name: 'navigation',
    props: ["navigation"],
    methods: {
        whiteSpace: function (a) {
            return a.toLowerCase().trim();
        }
    }
}

So I found out the problem. 所以我找出了问题所在。 .trim() function doesn't work, I tried .replace(/\\s/g, ""); .trim()函数不起作用,我尝试过.replace(/\\s/g, ""); and it helps, thank you for your help guys :) 它会有所帮助,谢谢您的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM