12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Nuxt.js/Vue.js】 手軽に長い文字列を省略して末尾を三点リーダー(…)にする方法

Posted at
plugins/filter.js
Vue.filter('truncate', function(value, length, omission) {
  var length = length ? parseInt(length, 10) : 20;
  var ommision = omission ? omission.toString() : '...'

  if(value.length <= length) {
    return value;
  }
  else {
    return value.substring(0, length) + ommision;
  }
})
nuxt.config.js
  plugins: [
    '~/plugins/filter' // 追記する
  ],
text.vue
<div> {{ text | truncate(50) }} </div> // 引数に表示したい文字数を入れる
12
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?