vue3.0 + elementUI 弹窗二次封装

 使用组件:

 <page-dialog v-model:dialogShow="gisLock" footerCustom>
        {{gisLock}}
 </page-dialog>


 <page-dialog v-model:dialogShow="gisLock">
        {{gisLock}}
        <template #dialog-button>
          <el-button type="primary" plain @click="handleClose">
            <el-icon class="el-icon--left">
              <Close />
            </el-icon>返回
          </el-button>
        </template>
 </page-dialog>

 组件封装:

<template v-if="dialogVisible">
  <el-dialog v-model="dialogVisible" :width="width" :before-close="handleClose">
    <template #header>
      <div class="dialogTitle">
        // 弹窗名称前面都有一个icon  名称不icon也不一样, #header 可以根据自己的需求调整
        <el-icon v-if="header.icon" :class="selectClass()"></el-icon>
        <span>{{ header.title }}</span>
      </div>
    </template> 
    <slot></slot>  
     <template #footer v-if="footerCustom">
      <div class="dialog-footer">
        <el-button type="primary" plain @click="handleClose">
          <el-icon class="el-icon--left">
            <Close />
          </el-icon>取消
        </el-button>
        <el-button type="primary" @click="saveBtnFn">
          <el-icon class="el-icon--left">
            <Check />
          </el-icon>确定
        </el-button>
      </div>
    </template>
    <template #footer v-else>
      <div class="dialog-footer">
        <slot name="dialog-button"></slot>
      </div>
    </template> 
  </el-dialog>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, watch } from "vue";
export default defineComponent({
  name: "PageDialog",
  props: {
    dialogShow: {
      type: Boolean,
      default: false,
    },
    header: {
      type: Object,
      default: {
        icon: "",
        title: "",
      },
    },
    width: {
      type: String,
      default: "40%",
    },
    footerCustom: {
      type: Boolean,
      default: false,
    },
  },  
  setup(props, { emit }) {
    // 数据
    const state = reactive({
      dialogVisible: false,
    });
    // 方法
    const methods = reactive({ 
      selectClass: () => {
        let icon = "";
        if (props.header.icon) {
          icon = "iconfont " + props.header.icon;
        }
        return icon;
      },
      handleClose: () => {
        state.dialogVisible = false;
      },
      saveBtnFn: () => {
        emit("saveBtnFn");
      },
    });
    watch(
      () => state.dialogVisible,
      (val) => { 
        //查看子组件值是否变化,在赋值给父组件
        emit("update:dialogShow", val);
      }
    );
    watch(
      () => props.dialogShow,
      (val) => {
        //查看父组件值是否变化,在赋值给子组件
        state.dialogVisible = val;
      }
    );
    return {
      ...toRefs(state),
      ...toRefs(methods),
    };
  },
});
</script> 
<style scoped>
.dialog-footer{
    text-align: center;
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3.0是一个非常流行的JavaScript框架,不仅易于学习和使用,而且可以与许多UI框架集成。ElementUI是一种流行的UI框架,提供了许多易于使用的UI组件和效果。在Vue 3.0中,可以使用TypeScript(TS)编写代码,提供了更好的类型安全和代码可读性。下面是在Vue 3.0中引用ElementUI的步骤: 1. 安装ElementUI:可以使用npm或yarn命令安装ElementUI ``` npm i element-plus -S ``` 2. 使用TypeScript的话需要安装依赖 `ts` 和 `webpack`(如果没有的话) ``` npm i webpack webpack-cli webpack-dev-server typescript ts-loader -D ``` 3. 在Vue项目中引入ElementUI样式和组件: 3.1 引入样式(会自动挂载到全局样式表上) ```scss // main.ts import 'element-plus/dist/index.css' ``` 3.2 引入组件 ```js // main.ts or other entry file import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus' import 'element-plus/styles/index.css' // 引入组件样式 const app = createApp(App) app.use(ElementPlus) // 注册全局组件 app.mount('#app') ``` 这样就可以通过引入ElementPlus来使用ElementUI组件了。例如,在Vue 3.0中使用一个按钮组件: ```vue <template> <el-button type="primary">click me</el-button> </template> <script lang="ts"> import { defineComponent } from 'vue' import { ElButton } from 'element-plus' export default defineComponent({ name: 'MyButton', components: { ElButton } // 局部注册组件 }) </script> ``` 总的来说,引用ElementUI到Vue 3.0中相对比较简单,只需要安装依赖与组件后进行注册即可正常使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值