非前后端分离项目使用vue继承,提取公共方法和filters

var vue_fn = {
    filters:{
        formatDate: function (value) {
            if(value==null){
                return "";
            }
            let date = new Date(value);
            let year = date.getFullYear();
            let month = date.getMonth()+1;
            let day = date.getDate();
            month = month < 10 ? "0"+month:month;
            day = day < 10 ? "0"+day:day;
            date = year+'-'+month+'-'+day;
            return date;
        },
        formatDateTime: function (value) {
            if(value==null){
                return "";
            }
            let date = new Date(value);
            let year = date.getFullYear();
            let month = date.getMonth()+1;
            let day = date.getDate();
            let h = date.getHours();
            let mm = date.getMinutes();
            let s = date.getSeconds();
            month = month < 10 ? "0"+month:month;
            day = day < 10 ? "0"+day:day;
            date = year+'-'+month+'-'+day+" "+h+":"+mm+":"+s;
            return date;
        }
    },
    methods: {
        showPhoto: function (title, url) {//显示图片
            var json = {
                "data": [{
                    "alt": title,
                    "src": url,
                }]
            }
            layer.photos({
                photos: json //格式见API文档手册页
                // ,anim: 1 //0-6的选择,指定弹出图片动画类型,默认随机
            });
        }
    }
}

子vue,filter可以直接用,method也可以直接this.method名调用

var childvue1 = new Vue({
    extends : vue_fn,
    el: '#settleDetailDev',
    data: {
    } ,
    mounted: function () {
    },
    methods: {
       showFkpz:function () {
          var _this = this;
          _this.showPhoto("付款凭证","url");
        }
    }
})

  

  

原文地址:https://www.cnblogs.com/mafy/p/11897749.html