Vue 3 迁移策略笔记—— 第11节:移除过滤器

编程入门 行业动态 更新时间:2024-10-23 18:33:09

Vue 3 迁移策略笔记—— 第11节:移除<a href=https://www.elefans.com/category/jswz/34/1771166.html style=过滤器"/>

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将被移除且不再支持了。如果需要实现过滤功能,建议通过methodcomputed属性来实现。

上面的例子可改为:

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

全局过滤器

如果我们需要使用全局过滤器。那么在每个单独组件中使用methodcomputed属性来实现 将很不便利。因此,为此,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节:移除过滤器

本文发布于:2024-03-13 02:51:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1733021.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   移除   策略   笔记   Vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!