惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Recent Announcements
Recent Announcements
Jina AI
Jina AI
G
Google Developers Blog
腾讯CDC
博客园_首页
博客园 - 【当耐特】

博客园 - 前端架构师

DSH 插件第二弹生态全景:11 个插件,把 DeepSeek 变成一个完整的 Coding Agent 平台 DSH 插件推荐清单:DeepSeek Harness 必装的 14 个插件 手摸手教你做一个codex桌面宠物 告别信息碎片化!我把 Claude Code、Cursor、MCP 和 Vibe Coding 整理成了一座全能 AI 宝库(建议收藏) Claude Code 源码架构分析 我不是狐狸,我是那Harness Engineering 小龙虾的自我养成之路 vue3+ts添加公共富文本组件 vue watch详解 微信授权没搞过? css3图片防止变形 公众号使用微信sdk的正确姿势 web离线应用前提之离线检测 es6手写一个call 同学给我来个手写的new吧 this综合篇 this绑定番外篇之箭头函数绑定 this详解下 012天this详解上 011天之跨域资源共享CORS 010天JSON.stringify()详解 009天之跨浏览器的事件处理程序 使用XHR上传文件要不要了解一下?
vue中引入公用过滤器?
前端架构师 · 2020-03-22 · via 博客园 - 前端架构师

比如我们封装一个金钱的过滤器:

不废话,直接上代码

在main.js平级新建filter文件夹,里面新建index.js和money.js

index.js

import {
    moneyP
} from './money'

export default moneyP;

注意这里不要用module.exports导出了,会报错。

// module.exports = {
//     normalTime
// }

money.js里面

function fmoney(s){   
    let n = 2;
    n = n > 0 && n <= 20 ? n : 2;   
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";   
    var l = s.split(".")[0].split("").reverse(),   
    r = s.split(".")[1];   
    let t = "";   
    for(let i = 0; i < l.length; i ++ )   
    {   
       t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");   
    }   
    return t.split("").reverse().join("") + "." + r;   
 } 
 
export const moneyP = (num) => {
    if(num == 0) {
        let outnum = '0.00'
        return outnum;
    } else {
        if(num != '') {
            if(num < 0) {
                let outnum = fmoney(-num)
                return '-' + outnum
            } else {
                let outnum = fmoney(num)
                return outnum
            }
        }
    }
}

在main.js里面引入:

//引入过滤器
import filters from './filter'
Vue.filter('moneyP',filters)

//Vue.filter(名字,函数)

使用时候在你的组件中直接用就可以了

 <div>{{numTotal | moneyP}}元</div>

扫码加群,每日更新一篇前端技术文章一起成长。