vue2.0组件代码优化整理

//组件1
 Vue.component('auto-list', {
            template: '#autoListText',//不同
            data: function () {
                return {
                    lists: [],
                    limit: 5
                }
            },
            props: {
                page: {
                    type: Number,
                    default: 1
                },
                catid: {
                    type: Number,
                    default: 20
                },
                loading: {
                    type: Boolean,
                    default: false
                }
            },
            created: function () {
                this.getList()
            },
            methods: {
                getList: function () {
                    $("#loading").removeClass('hide');
                    projectModel.getList(this.catid, this.page, this.limit, this.getData)
                },
                getData: function (data) {
                    var _this = this;
                    if (data.code == 1) {
                        if (!common.isNullOrEmpty(data.info)) {
                            var page = parseInt(data.info.pages.page);
                            var pagecount = parseInt(data.info.pages.pagecount);
                            $.each(data.info.list, function(i,item){
                             _this.lists.push(item);
                            });
                            NProgress.done();
                            if(page == pagecount){
                                this.$emit('restload', true);
                            }else{
                                this.$emit('restload', false);
                            }
                        }
                    }
                }
            },
            updated: function () {
                $("#loading").addClass('hide');
                timer.loadStartTimer();//不同
                timer.loadCircliful();
            },
            watch: {
                page : function () {
                    this.getList();
                }
            }
        });
    

//组件2

    Vue.component('stock-list', {
        template: '#stockListText',/不同
        data: function () {
            return {
                lists: [],
                limit: 5
            }
        },
        props: {
            page: {
                type: Number,
                default: 1
            },
            catid: {
                type: Number,
                default: 20
            },
            loading: {
                type: Boolean,
                default: false
            }
        },
        created: function () {
            this.getList()
        },
        methods: {
            getList: function () {
                $("#loading").removeClass('hide');
                NProgress.start();
                projectModel.getList(this.catid, this.page, this.limit, this.getData)
            },
            getData: function (data) {
                var _this = this;
                NProgress.done();
                if (data.code == 1) {
                    if (!common.isNullOrEmpty(data.info)) {
                        var page = parseInt(data.info.pages.page);
                        var pagecount = parseInt(data.info.pages.pagecount);
                        $.each(data.info.list, function(i,item){
                            _this.lists.push(item);
                        });
                        if(page == pagecount){
                            this.$emit('restload', true);
                        }else{
                            this.$emit('restload', false);
                        }
                    }
                }
            }
        },
        updated: function () {
            $("#loading").addClass('hide');
            timer.loadEndTimer();//不同
            timer.loadCircliful();
        },
        watch: {
            page : function () {
                this.getList();
            }
        }
    })
    

以上两个组件的代码非常相似,共同部分该如何优化合并最合理呢?

阅读 3.1k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题