Mixins simplify common operations

Strikethrough format We often encounter operations such as keeping two decimal places for money and converting timestamps during development. Each time we will write a public function, and then filter in the filters in the page. This method is annoying every time, but every time I feel that I need to use it, I have to write it in the filters again! ! ! However, the ultimate pursuit of our apes is lazy, then how can this work~~~ Brothers, copy guys! On mixins! ! !

import {
    
     u_fixed } from './tool'

const mixins = {
    
        
    filters: {
    
            
        // 保留两位小数        
        mixin_fixed2 (val) {
    
                
            return u_fixed(val)        
        },
        // 数字转汉字,16000 => 1.60万        
        mixin_num2chinese (val) {
    
                
            return val > 9999 ? u_fixed(val/10000) + '万' : val;        
    }    
}}
export default mixins

Create a new mixins.js, write all the content we need to mix in it, for example, mix in filters here, write several commonly used operations in it, you can extend it yourself. In this case, import this js on the page we need, and then declare the mix-in, and then it can be used in a normal way. For example, I can now use our filtering operation directly on the page

{
    
    {
    
    1000 | mixin_fixed2}}

Guess you like

Origin blog.csdn.net/diaojw090/article/details/101680744