Encapsulate el-dialog as a componentWhen 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 errorBut 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 mixinsWhen 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:
|
>>: Two tools for splitting the screen in the Linux command line terminal
Preface Recently, I have been busy dealing with s...
Although Mac systems come with PHP and Apache, so...
This article records the installation and configu...
Many people may ask, does the text on the website...
Table of contents Preface 1. Create a new Vue pro...
/******************** * Virtual File System VFS *...
1. Introduction When we are writing a page, we so...
By adding the current scroll offset to the attrib...
Some projects have relatively simple business, bu...
The fd command provides a simple and straightforw...
Optimizing and refining information is always the ...
Without further ado, I will post the code for you...
CSS3 can create animations, which can replace man...
1. Introduction to Linux .NET Core Microsoft has ...
Table of contents Preface start step Troubleshoot...