src/utils/index.ts
export const _scopeText = (str: string, len: number): string => {
if (str.length <= len) {
return str
}
return `${str.slice(0, len)}...`
}
export const _money = (number: number, mark: string): string => {
return `${mark}${number}`
}
main.ts
import { createApp } from 'vue'
import App from './App.vue'
import * as tools from './utils/index'
const app = createApp(App)
app.config.globalProperties.$filter = tools
app.mount('#app')
使用
<div>{{ $filter._scopeText(name, 10) }}</div>
<div>{{ $filter._scopeText(text, 30) }}</div>
<div>{{ $filter._money(33, '$') }}</div>
这篇博客介绍了如何在Vue应用中创建并使用自定义过滤器`_scopeText`来截取字符串并显示省略号,以及`_money`过滤器用于格式化数字为货币形式。示例展示了在模板中如何调用这些过滤器来处理`name`和`text`变量以及显示货币金额。
1353

被折叠的 条评论
为什么被折叠?



