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
Let me first explain why the text is not vertical...
Table of contents 1. Installation and operation o...
In the front-end layout process, it is relatively...
In daily development tasks, we often use MYSQL...
This article example shares the specific code for...
This article uses examples to describe the common...
Mind Map He probably looks like this: Most of the...
1. Purpose: Make the code easier to maintain and ...
There are some tags in XHTML that have similar fu...
This article shares with you the tutorial of inst...
js interesting countdown case, for your reference...
=================================================...
background This article mainly tests whether the ...
Technical Background This application uses the vu...
Table of contents explain: Summarize Replenish Un...