Vue.js:大写不起作用

mat*_*teo 5 javascript uppercase vue.js

我有这个代码:

data: {
    cols: ['nome', 'data', 'size', 'ext'],
    items: []
},
Run Code Online (Sandbox Code Playgroud)

我需要将文本转换为大写.我按照官方网站的例子尝试了这种方式:

<th v-for="col in cols">
  {{col | uppercase}}
</th>
Run Code Online (Sandbox Code Playgroud)

但是,文本仍为小写.你知道为什么吗??

Kau*_*win 15

有另一种更简单的方法可以做到这一点.您可以直接在双括号表示法中使用Javascript,而不是使用@ wostex的过滤器方法.

new Vue({
  el: '#app',
  data: {
    cols: ['nome', 'data', 'size', 'ext']
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
  <table>
    <thead>
      <th v-for="col in cols">
        {{ col.toUpperCase() }}
      </th>
    </thead>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @SweetChillyPhilly`word.charAt(0).toUpperCase()+ word.substr(1)`。大写第一个字母,然后追加字符串减去第一个字符。 (2认同)
  • @SweetChillyPhilly另一种替代方法是使用CSS`text-transform:captitalize`。 (2认同)

Ego*_*kio 11

Vue.js sinse 2.x中没有内置过滤器,因此您需要手动定义过滤器:

new Vue({
  el: '#app',
  data: {
    cols: ['nome', 'data', 'size', 'ext']
  },
  filters: {
    uppercase: function(v) {
      return v.toUpperCase();
    }
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<div id="app">
  <table>
    <thead>
      <th v-for="col in cols">
        {{col | uppercase}}
      </th>
    </thead>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)