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

Detailed Introduction to Nginx Installation and Configuration Rules

Table of contents 1. Installation and operation o...

Summary of Several Methods for Implementing Vertical Centering with CSS

In the front-end layout process, it is relatively...

mysql group by grouping multiple fields

In daily development tasks, we often use MYSQL...

Mini Program to Implement Slider Effect

This article example shares the specific code for...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

Vuex modularization and namespaced example demonstration

1. Purpose: Make the code easier to maintain and ...

Similar to HTML tags: strong and em, q, cite, blockquote

There are some tags in XHTML that have similar fu...

VMWare Linux MySQL 5.7.13 installation and configuration tutorial

This article shares with you the tutorial of inst...

js to achieve interesting countdown effect

js interesting countdown case, for your reference...

Common failures and reasons for mysql connection failure

=================================================...

Vue-cli framework implements timer application

Technical Background This application uses the vu...

A brief discussion on the principle of Vue's two-way event binding v-model

Table of contents explain: Summarize Replenish Un...