简体   繁体   English

Nuxt.js 使用 vue 过滤器插件不起作用

[英]Nuxt.js using vue filter plugin does not work

I'm trying to use a Vue filter with Nuxt and i just can't get it to work.我正在尝试在 Nuxt 中使用 Vue 过滤器,但我无法让它工作。

plugins/filters.js插件/filters.js

import Vue from 'vue'

/** Vue Filters Start */
Vue.filter('truncate', function (text, length, suffix) {
  if (text.length > length) {
    return text.substring(0, length) + suffix
  } else {
    return text
  }
})
/** Vue Filters End */

nuxt.config.js nuxt.config.js

  plugins: [
    { src: '~/plugins/filters.js' }
  ],

Vue file 

    <h2 class="m-0">
      {{ selected ? (selected.name | truncate(10, '...') ) : 'Place' }}
    </h2>

The error i'm getting我得到的错误

_vm.truncate is not a function

If anyone stumbles upon a similar problem, it seems that Vue doesn't like the Filter being used inside the terniary operator.如果有人偶然发现类似的问题,那么 Vue 似乎不喜欢在三元运算符内部使用 Filter 。 Instead you can append the filter to the result of the operator so {{ (selected ? selected.name : 'Place') |相反,您可以将过滤器附加到运算符的结果中,所以 {{ (selected ? selected.name : 'Place') | truncate(10, '...') }} does work. truncate(10, '...') }} 确实有效。

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

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