In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to achieve a pop-up plug-in in vue". In daily operation, I believe many people have doubts about how to achieve a pop-up plug-in in vue. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve a pop-up plug-in in vue". Next, please follow the editor to study!
Popup.vue
{{text}}
Component html structure, outer divposition:fixed positioning, inner div display pop-up content
Export default {name: 'popup', props: {text: {/ / text content type: String, default:''}, time: {/ / display duration type: Number, default: 3e3},}, data () {return {visible: false}}, methods: {open () {this.visible = true clearTimeout (this.timeout) This.$emit ('show') if (this.time > 0) {this.timeout = setTimeout (() = > {this.hide ()}, this.time)}}, hide () {this.visible = false this.$emit (' hide') clearTimeout (this.timeout);}
Popup.vue has only two properties: text and display time. Component display hiding is controlled by the internal property visible, which only exposes external open and hide2 methods, and two methods trigger the corresponding events.
Test it
Import Popup from'@ / components/popup'... This.$refs.popup.open ()...
Plug-in
The component functionality is written, but this invocation method is cumbersome. For example, the call to layer.js is layer.open (...). No import, no ref, of course, you have to refer to layer globally first. Can the pop-up window we write be so convenient? for this, we need to rewrite popup into a vue plug-in.
It is a plug-in, but it is the component itself that can configure the property call method, specifically the instantiated component, and this instance must be a global singleton, so that different vue files will not fight when calling popup.
Generate a singleton
/ / plugins/popupVm.jsimport Popup from'@ / components/popup'let $vmexport const factory = (Vue) = > {if (! $vm) {let Popup = Vue.extend (PopupComponent) $vm = new Popup ({el: document.createElement ('div')}) document.body.appendChild ($vm.$el)} return $vm}
After the component is instantiated, it is added to body. Props cannot be written in html and needs to be controlled by js. Here, write a method to let the default value of the attribute continue to work.
/ / plugins/util.jsexport const setProps = ($vm, options) = > {const defaults = {} Object.keys ($vm.$options.props) .forEach (k = > {defaults [k] = $vm.$ options.props.default}) const _ options = _ .cards ({}, defaults) Options) for (let i in _ options) {$vm.$ props [I] = _ options [I]} / plugins/popupPlugin.jsimport {factory} from'. / popupVm'import {setProps} from'. / util'export default {install (Vue) {let $vm = factory (Vue) Const popup = {open (options) {setProps ($vm, options) / / listening event typeof options.onShow = = 'function' & & $vm.$once (' show', options.onShow); typeof options.onHide = = 'function' & & $vm.$once (' hide', options.onHide); $vm.open () }, hide () {$vm.hide ()}, / / configure only the text text (text) {this.open ({text})}} Vue.prototype.$popup = popup}}
Register plug-ins in main.js
/ / main.jsimport Vue from 'vue'import PopupPlugin from' @ / plugins/popupPlugin'Vue.use (PopupPlugin) is very convenient to call within the vue framework. This.$popup.text ('pop-up message'). At this point, the study on "how to implement a pop-up plug-in in vue" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.