Vue封装工具栏

1、在页面如html、jsp重复写着相同的代码,挺繁琐,而且不便于维护,以下尝试自己学习如何封装工具栏,比如四个按钮:添加删除、修改、查询等,后面可以自己扩展

2、工具栏js

/**公共工具栏*/
Toolbar = function(config) {
    //config是调用时候,传递过来的参数
	this.config = config;
	this.render = function() {
		var html = [];
		html.push("<div>");
		html.push("<template v-for=\"button in options.items\" >");
		html.push("<el-button type=\"primary\"  size=\"mini\"  @click=\"button.handler\" icon=\"button.icon\">{{button.text}}</el-button>");		
		html.push("</template>");
		html.push("</div>");
		var app = new (Vue.extend({
			template:html.join(""),
			data:function() {
				return {
					options:config
				};
			}
		}))().$mount($("#" + this.config.renderTo)[0]);
	
	};
};

3、主页面js

   //主页面,工具栏实例化
	var toolbar = new Toolbar({
		"renderTo":"toolbar", 
		"items":[
		{
			type:"button", 
			text:"查询", 
			useable:"T", 
			handler:function () {
				APP.$options.methods.doSearch(1);
			}
		},
		{
			type:"button", 
			text:"新增", 
			useable:"T", 
			handler:function () {
				addOsuser();
			}
		},
		{
			type:"button", 
			text:"修改", 
			useable:"T", 
			icon:"el-icon-download",
			handler:function () {
				updateOsuser();
			}
		},
		{
			type:"button", 
			text:"删除", 
			bodyStyle:"delete", 
			useable:"T", 
			icon:"el-icon-download",
			handler:function () {
				deleteOsuser();
			}
		}]
	}).render();

4、html工具栏代码

<el-row style="margin-bottom: 5px;">
			<el-col :span="24">
				<div id="toolbar"></div>
			</el-col>
</el-row>

总结:将繁琐的html代码,比如button 标签啊 超链接标签之类的,重复繁琐的代码,放到一个公共js里调用即可,可能几个html,你感觉工作量还不大,但是当一个系统有上百个html需要维护的时候,你才会发现封装起来的好处。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值