Create a global toast component
src -> components -> Toast ->
<template> <transition name="fades"> <div class="Errormes" v-if="show">{{txt}}</div> </transition> </template> <script> export default { name: 'Toast', data () { return { txt: '', show: false } } } </script> <style lang="less" scoped> .fades-enter-active, .fades-leave-active { transition: opacity 0.5s; } .fades-enter, .fades-leave-active { opacity: 0; } /* Prompt box */ .Errormes { position: fixed; top: 40%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); padding: 20px 30px; background: rgba(0, 0, 0, 0.8); border-radius: 16px; color: #fff; font-size: 28px; z-index: 999999; max-width: 80%; height: auto; line-height: 60px; text-align: center; } </style>
src -> components -> Toast ->
import Toast from './' const toast = {} = (vue) => { const ToastClass = (Toast) const instance = new ToastClass() instance.$mount(('div')) (instance.$el) let t = null const ToastMain = { show (msg, seconds = 2000) { if (t) clearTimeout(t) = msg = true t = setTimeout(() => { = false }, seconds) } } .$toast = ToastMain } export default toast
import Vue from 'vue' import App from './' import toast from '@/components/Toast/index' (toast)
use
$(message)
About the application of understanding
Basic concepts
( options )
Using the basic Vue constructor, create a "subclass". A parameter is an object containing component options.
Generally, we will use the received component object to create a constructor, then use the created constructor new instance, and mount this instance on an element.
Basic examples of the official website
<div ></div> // Create a constructorvar Profile = ({ template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>', data: function () { return { firstName: 'Walter', lastName: 'White', alias: 'Heisenberg' } } }) // Create a Profile instance and mount it on an element.new Profile().$mount('#mount-point')
The data option is a special case, please note - in () it must be a function
The results are as follows:
<p>Walter White aka Heisenberg</p>
application
We rarely use the global API that belongs to Vue in actual business development because the steps to use extend are more cumbersome than the commonly used writing method.
However, in some independent component development scenarios, for example, to implement a component similar to () prompt, it is required to call it like calling a JS function. At this time, the combination of + $mount is what we need to pay attention to.
1. Description of the difference between vue $mount and el
Before applying, let’s first learn about two things - $mount and el. There is no difference in the use effect between the two, and they are all for mounting the instantiated vue into the specified dom element.
If el is specified when instantiating vue, the vue will be rendered in the dom corresponding to this el. Otherwise, if el is not specified, the vue instance will be in a "unmounted" state, and the mount can be manually performed through $mount.
Note: If $mount does not provide parameters, the template will be rendered as an element outside the document, and you must insert it into the document using the native DOM API.
var MyComponent = ({ template: '<div>Hello!</div>' }) // Create and mount to #app (replaces #app)new MyComponent().$mount('#app') // Same as abovenew MyComponent({ el: '#app' }) // Or, render outside the document and then mountvar component = new MyComponent().$mount() ('app').appendChild(component.$el)
2. Implement the Loading plugin ($mount)
<div > <button @click="showLoading">showLoading</button> </div> function Loading(msg) { // Create a constructor const Loading = ({ template: '<div >{{ msg }}</div>', props: { msg: { type: String, default: 'loading' } }, name: 'Loading' }) // Create mount div const div = ('div') ('id', 'loading-div') (div) // Create an instance and mount it on an element new Loading().$mount('#loading-div') // Return a function that removes elements return () => { (('loading-div')) } } // Mount to vue instance.$loading = Loading new Vue({ el: '#root', methods: { showLoading() { const hide = this.$loading('Loading, please wait...') setTimeout(() => { hide() }, 1500) } } })
3. Implement information pop-up plug-in (el)
Create a new one
<template> <div v-if='flag'> <div class="contents" > <div class="content-top">{{ }}</div> <div class="content-center">{{ }}</div> <div class="content-bottom"> <button @click='show' class="left">{{ }}</button> <button @click='hide' class="right">{{ }}</button> </div> </div> </div> </template> <script> export default { data () { return { flag: true, text: { title:'title', msg: 'This is an information pop-up component', btn: { ok: 'Sure', no: 'Cancel' } } } }, methods: { show (){ = false; }, hide() { = false; } } } </script>
Create a new one
import Vue from 'vue' import PopBox from './' // Returns the constructor created by an instance, but the instance constructor needs to be mounted to the pagelet popBox = (PopBox) = (vue, text) => { // Dynamically create a div element in the body, and then this div will be replaced with the contents of the entire vue file // At this time, popBoxDom is generally equivalent to the entire component object, and the data in the component is used by the object calling the attribute method to use the data in the component let popBoxDom = new popBox({ el: ('div') }) // The created component instance can be accessed through the $el attribute (popBoxDom.$el) // Pass the text content that needs to be passed to the component instance = text; // Return a promise, perform asynchronous operations, return when successful, return when failure return new Promise((res, rej) => { = () => { res() //Return operation when correct = false; } = ()=>{ rej() //Return operation on failure = false; } } .$popBox = popBox }) } // Export and expose logical functionsexport default popBox
Page usage
import PopBox from './' (popBOx); this.$popBox({ title:'title', msg:'content', btn:{ ok:'Sure', no:'Cancel'} }).then(()=>{ ('ok') }).catch(()=>{ ('no') })
other
import toastCom from "./Toast"; const toast = {}; = vue => { const ToastCon = (toastCom); const ins = new ToastCon(); ins.$mount(("div")); (ins.$el); () .$toast = ; }; const globalComponent = { install: function(Vue) { (toast); } }; export default globalComponent;
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.