uni-app 百度小程序 基本调试库 3.260 以上版本 不支持全局过滤器
uni-app 百度小程序 基本调试库 3.260 以上版本 不支持全局过滤器
操作步骤:
- 写个全局过滤器测试一下就可以了。
预期结果:
- 正常渲染
实际结果:
- 许安然处undefined
bug描述:
```javascript
Vue.filter('priceFormat', (price, fixed=0)=>{
if(!price || isNaN(price)){
return 0
}
return fixed > 0 ? parseFloat(price).toFixed(fixed) : parseFloat(price).toFixed(2);
})
<view class="price-cls">价格:{{price | priceFormat}}</view>
全局过滤器可以进去,但是渲染出来是undefined。 百度基础调试库调低版本到3.250 只下 是可以正常渲染的。

| 项目信息 | 版本/方式 |
|------------------|-------------|
| 产品分类 | uniapp/小程序/百度 |
| PC开发环境操作系统 | Mac |
| PC开发环境操作系统版本号 | win10 |
| HBuilderX类型 | 正式 |
| HBuilderX版本号 | 3.1.4 |
| 第三方开发者工具版本号 | 3.25.1 |
| 基础库版本号 | 3.260 |
| 项目创建方式 | HBuilderX |
更多关于uni-app 百度小程序 基本调试库 3.260 以上版本 不支持全局过滤器的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
问题已确认,预计在下一个 alpha 版本修复
更多关于uni-app 百度小程序 基本调试库 3.260 以上版本 不支持全局过滤器的实战教程也可以访问 https://www.itying.com/category-93-b0.html
HBuilderX 3.1.6+ alpha 已修复
在百度小程序基础库3.260及以上版本中,确实不再支持Vue全局过滤器。这是因为百度小程序平台从该版本开始移除了对Vue.filter的兼容性支持。
解决方案:
- 改用计算属性或方法处理数据:
computed: {
formattedPrice() {
const price = this.price;
if(!price || isNaN(price)) return '0';
return parseFloat(price).toFixed(2);
}
}
模板中使用:
<view class="price-cls">价格:{{formattedPrice}}</view>
- 使用局部过滤器(在页面或组件内定义):
filters: {
priceFormat(price, fixed = 0) {
if(!price || isNaN(price)) return '0';
return fixed > 0 ? parseFloat(price).toFixed(fixed) : parseFloat(price).toFixed(2);
}
}
- 封装工具函数:
// utils/formatter.js
export function priceFormat(price, fixed = 0) {
if(!price || isNaN(price)) return '0';
return fixed > 0 ? parseFloat(price).toFixed(fixed) : parseFloat(price).toFixed(2);
}