过滤器"/>
Vue 3 迁移策略笔记—— 第11节:移除过滤器
前言
本笔记主要基于官方文档《迁移策略—— 过滤器》汇总而来。如有理解出入,请以官方文档为主。建议您以官方文档为主,本文为辅。这样您可以“以自己为主”审视的阅读,从而不被我的观点带偏。
概述
Vue 3.x 将移除且不再支持 filters
。
Vue 2.x 的 filters
在 Vue 2.x 中,我们可以借助filters
来处理通用文本格式。如下:
<template><h1>Bank Account Balance</h1><p>{{ accountBalance | currencyUSD }}</p>
</template><script>export default {props: {accountBalance: {type: Number,required: true}},filters: {currencyUSD(value) {return '$' + value}}}
</script>
尽管看起来很方便,但是它需要一个自定义语法。这就打破了"{{ }}
内的表达式只能是 Javascript"的设想,而且增加了学习成本和实现成本。不仅有学习成本,而且有实现成本。
Vue 3.x 移除 filters
在 Vue 3.x,filters
将被移除且不再支持了。如果需要实现过滤功能,建议通过method
或computed
属性来实现。
上面的例子可改为:
<template><h1>Bank Account Balance</h1><p>{{ accountInUSD }}</p>
</template><script>export default {props: {accountBalance: {type: Number,required: true}},computed: {accountInUSD() {return '$' + this.accountBalance}}}
</script>
全局过滤器
如果我们需要使用全局过滤器。那么在每个单独组件中使用method
或computed
属性来实现 将很不便利。因此,为此,Vue 3.x 提供了globalProperties
。我们可以借助globalProperties
来注册全局过滤器:
const app = createApp(App)app.config.globalProperties.$filters = {currencyUSD(value) {return '$' + value}
}
然后这样使用:
<template><h1>Bank Account Balance</h1><p>{{ $filters.currencyUSD(accountBalance) }}</p>
</template>
注意,全局过滤器里面定义的只能是
method
。Note that with this approach, you can only use methods, not computed properties, as the latter only make sense when defined in the context of an individual component.
本系列目录
-
Vue 3 迁移策略笔记—— 第1节:v-for 中的 Ref 数组
-
Vue 3 迁移策略笔记—— 第2节:Async Components 异步组件
-
Vue 3 迁移策略笔记—— 第3节:Attribute Coercion Behavior (属性强制行为)
-
Vue 3 迁移策略笔记——第4节:$attrs 包括class&style
-
Vue 3 迁移策略笔记—— 第5节:移除 $children
-
Vue 3 迁移策略笔记—— 第6节:自定义指令
-
Vue 3 迁移策略笔记—— 第7节:自定义元素交互
-
Vue 3 迁移策略笔记—— 第8节:Data 选项
-
Vue 3 迁移策略笔记—— 第9节:新增 emits 选项
-
Vue 3 迁移策略笔记—— 第10节:事件 API
-
Vue 3 迁移策略笔记—— 第11节:移除过滤器
-
Vue 3 迁移策略笔记—— 第12节:片段
-
Vue 3 迁移策略笔记—— 第13节:函数式组件
-
Vue 3 迁移策略笔记—— 第14节:全局 API
-
Vue 3 迁移策略笔记—— 第15节:全局 API 的 tree shaking
-
Vue 3 迁移策略笔记—— 第16节:Inline Template 属性
-
Vue 3 迁移策略笔记—— 第17节:Key 属性
-
Vue 3 迁移策略笔记—— 第18节:按键修饰符
-
Vue 3 迁移策略笔记—— 第19节:移除 $listeners
-
Vue 3 迁移策略笔记—— 第20节:Props 的默认值函数不能访问this
-
Vue 3 迁移策略笔记—— 第21节:渲染函数 API
-
Vue 3 迁移策略笔记—— 第22节:Slots 的统一
-
Vue 3 迁移策略笔记—— 第23节:Transition Class 的变化
-
Vue 3 迁移策略笔记—— 第24节:Transition Group 不再需要设置根元素
-
Vue 3 迁移策略笔记—— 第25节:v-on.native修饰符被移除
-
Vue 3 迁移策略笔记—— 第26节:在组件上使用 v-model 的变化
-
Vue 3 迁移策略笔记—— 第27节:v-if 和 v-for 的优先级
-
Vue 3 迁移策略笔记—— 第28节:v-bind 合并行为
-
Vue 3 迁移策略笔记—— 第29节:数组的监听
更多推荐
Vue 3 迁移策略笔记—— 第11节:移除过滤器
发布评论