nuext .js使用vue过滤器插件不工作



我正在尝试使用Vue过滤器与next,我只是不能让它工作。

plugins/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

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

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

我得到的错误

_vm.truncate is not a function

如果有人偶然发现类似的问题,那么似乎Vue不喜欢在三元操作符中使用Filter。相反,您可以将过滤器附加到操作符so的结果{{(选定?selected.name:"地方")|截断(10 , '...') }} 工作。

最新更新