vue项目封装element-ui组件

1,准备一个抽屉组件popup.vue

<template>
	<div>
		<el-drawer
		  :title="id ? '修改' : '新增'"
			:wrapper-closable="false"
			:close-on-press-escape="false"
		  :visible.sync="visible"
		  direction="rtl"
		  :before-close="handleClose"
			 @close="closeDrawer()"
			>
		  <span>内容</span>
		</el-drawer>
	</div>
</template>

<script>
	 export default {
	    data() {
	      return {
	        visible: false,
					id:null
	      };
	    },
	    methods: {
			initDrawer: function(id) {
			  this.id = id
			  this.visible = true
			},
	      handleClose:function(done) {
	        this.visible = false
	      },
		closeDrawer: function() {
		  this.id = null
		},
	    }
	  };
</script>

<style>
</style>

2,在其他vue文件中使用组件

<template>
  <div class="hello">
    <el-button type="primary" icon="el-icon-plus" @click="addOrUpdatePopupRight()">新增</el-button>
	<el-button type="danger" @click="addOrUpdatePopupRight(id)">修改</el-button>
	<!--3,使用组件 -->
    <popup-right v-if="popupRightVisible" ref="popupRight"/>
  </div>
</template>

<script>
    //1,引入
	import popupRight from '../components/popup.vue';
	export default {
		name: 'HelloWorld',
		components: { //2,挂载
		  popupRight
		},
		data() {
			return {
			  popupRightVisible: false,
			  id:1
			};
		},
		methods: {
			addOrUpdatePopupRight:function(id){
				this.popupRightVisible = true
				this.$nextTick(() => {
				  this.$refs.popupRight.initDrawer(id) //调用组件中的方法
				})
			},
			
		}
	}
</script>


<style>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2中,对于element-ui组件的二次封装,可以按照以下步骤进行: 1. 需求分析:明确需要封装element-ui组件,以及需要添加的功能和配置项。 2. 创建父组件:编写父组件的template和script代码,其中template中调用封装组件,script中定义需要传递给封装组件的props属性。 3. 创建封装组件:编写封装组件的template和script代码。在template中使用element-ui组件,并根据需要进行样式和布局的调整。在script中定义props属性,接收父组件传递的值,并监听element-ui组件的事件,触发update事件给父组件。 4. 通过临时变量传递值:由于父组件传递给封装组件的props不能直接作为封装组件的v-model属性传递给element-ui组件,所以需要在封装组件中定义一个临时变量来存储值,并将该变量与element-ui组件进行绑定。 5. 完成打通:在封装组件中监听中间件,接收到element-ui组件的update事件后,再将该事件传递给父组件。 总结来说,Vue2中对于element-ui组件的二次封装,需要创建父组件封装组件,通过props属性传递值,并在封装组件中监听element-ui组件的事件并触发update事件给父组件。同时,需要使用临时变量来传递值给element-ui组件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3+ts+element-plus 组件的二次封装-- 页脚分页el-pagination的二次封装](https://blog.csdn.net/cs492934056/article/details/128096257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值