Steps for encapsulating element-ui pop-up components

Steps for encapsulating element-ui pop-up components

Encapsulate el-dialog as a component

When we use element-ui, if a pop-up window contains a lot of content, we often encapsulate the pop-up window into a component, as follows:

<!-- DetailDialog.vue html -->
<template>
 <el-dialog title="Title" :visible.sync="visible" width="720px" >
  <p>Popup content</p>
 </el-dialog>
</template>
// DetailDialog.vue js
<script>
 props: {
  visible:
   type: Boolean,
   default: false
  }
 }
</script>

el-dialog will modify props and report an error

But there will be a problem. When the closing event inside el-dialog is triggered, such as clicking the pop-up shadow, it will emit an event to modify the props [visible] of the current component. Since the component cannot directly modify the prop attribute, an error will be reported.

We added an intermediate variable innerVisible to intercept the modification and acquisition of props [visible]

<!-- DetailDialog.vue html -->
<template>
 <el-dialog title="Title" :visible.sync="innerVisible" width="720px" >
  <p>Popup content</p>
 </el-dialog>
</template>
// DetailDialog.vue js
<script>
export default {
 props: {
  visible:
   type: Boolean,
   default: false
  }
 },
 computed: {
  innerVisible: {
   get: function() {
    return this.visible
   },
   set: function(val) {
    this.$emit('update:visible', val)
   }
  }
 }
}
</script>

In this way, when modifying prop[visible] inside el-dialog, we will notify the parent component through emit('update:') to avoid directly modifying props. Of course, the parent component needs to add the sync modifier to accept the modification:

<!-- father.vue html -->
<DetailDialog :visible.sync="detailVisible" />

So far, there is no problem with the encapsulated pop-up component.

Continue to optimize and use v-model to control display and hiding

// DetailDialog.vue js
<script>
export default {
 model: {
  prop: 'visible', // Modify the props name bound to v-model event: 'toggle' // Modify the custom event name bound to v-model },
 props: {
  visible:
   type: Boolean,
   default: false
  }
 },
 computed: {
  innerVisible: {
   get: function() {
    return this.visible
   },
   set: function(val) {
    this.$emit('update:toggle', val)
   }
  }
 }
}
</script>

After connecting to v-model, it is more elegant and neat to use.

<!-- father.vue html -->
<DetailDialog v-model="detailVisible" />

Continue to optimize and encapsulate into mixins

When pop-up components are frequently encapsulated, the above logic also needs to be copied constantly, so continue to optimize and encapsulate the above control display and hiding logic into mxins, which can be reused directly

// vModelDialog.js
export default {
 model: {
  prop: 'visible',
  event: 'toggle'
 },
 props: {
  visible:
   type: Boolean,
   default: () => {
    return false
   }
  }
 },
 computed: {
  innerVisible: {
   get: function() {
    return this.visible
   },
   set: function(val) {
    this.$emit('toggle', val)
   }
  }
 }
}

Then when encapsulating the pop-up plug-in, you only need to introduce mixins to complete the display and hide logic.

// DetailDialog.vue js
<script>
import vModelDialog from './vModelDialog.js'
export default {
 mixins: [vModelDialog],
}
</script>

The above is the detailed content of the steps of element-ui pop-up component packaging. For more information about element-ui pop-up component packaging, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue implements table pop-up component based on Element-ui
  • Solution to the problem of repeated pop-up of Element's Message pop-up window
  • The whole process of implementing the summary pop-up window with Vue+Element UI
  • Implementation of vue+elementui universal pop-up window (add+edit)
  • Solution to the failure of closing the dialog pop-up window in element-ui
  • Detailed explanation of the problem between pop-up windows using element ui and echarts in vue
  • Element modifies the implementation of the level of pop-up window components

<<:  Mysql5.7.14 installation and configuration method operation graphic tutorial (password problem solution)

>>:  Two tools for splitting the screen in the Linux command line terminal

Recommend

Summary of some common writing methods that cause MySQL index failure

Preface Recently, I have been busy dealing with s...

Install Apache2.4+PHP7.0+MySQL5.7.16 on macOS Sierra

Although Mac systems come with PHP and Apache, so...

MySQL 8.0.15 installation and configuration graphic tutorial

This article records the installation and configu...

Does the website's text still need to be designed?

Many people may ask, does the text on the website...

How to use Webstorm and Chrome to debug Vue projects

Table of contents Preface 1. Create a new Vue pro...

Linux kernel device driver virtual file system notes

/******************** * Virtual File System VFS *...

Use Smart CSS to apply styles based on the user's scroll position

By adding the current scroll offset to the attrib...

js to achieve simple front-end paging effect

Some projects have relatively simple business, bu...

A friendly alternative to find in Linux (fd command)

The fd command provides a simple and straightforw...

Dynamically add tables in HTML_PowerNode Java Academy

Without further ado, I will post the code for you...

css3 animation ball rolling js control animation pause

CSS3 can create animations, which can replace man...

Building .NET Core 2.0 + Nginx + Supervisor environment under Centos7 system

1. Introduction to Linux .NET Core Microsoft has ...