vue全局弹窗

摘要:
生成的虚拟dom需要装入才能成为真实的dom*/render:h=˃h//此处未指定主机元素,因为直接指定主体将替换现有内容。})$Mount()/*您可以获得真正的dom*/文档。身体appendChild/*获取组件实例*/constcomp=vm$children[0]/*添加销毁组件的方法*/comp。remove=function(){document.body.removeChildvm.$destroy()}returncomp}exportdefault{/*install该方法要求在使用组件时不需要传入组件模板,而只需传入参数,因此它是通过使用Coriolisation函数实现的。

效果:在任意页面,不需要引入组件,调用弹窗组件实例方法操作打开关闭 

1. 定义

1.1 定义弹窗页面

正常定义即可,需要定义方法show和hide控制组件变量显示及隐藏弹窗。

1.2 弹窗页面同级目录定义得Vue需要得js文件

注意: 搞了单例,调用多少次都是操作一个

import Vue from 'vue'
import FullLoginAd from './index.vue'

// 单例模式
let instance = null

function create (Component, props) {
    const vm = new Vue({
   /* render函数用来生成虚拟dom,接收一个createElement函数(一般称之h函数),
     并返回该函数的执行结果(生成的虚拟dom)。
      h函数接受三个参数,
      1.一个标签名或组件选项对象。
      2.与模板中属性对应的数据对象。
      3.子节点。
     生成的虚拟dom需要经过挂载($mount)才能变成真实dom
   */
    render: h => h(Component, { props })
    // 这里不指定宿主元素,因为直接指定body的话会将已有的内容替换掉。
    }).$mount() 
    /* 通过dom操作追加元素  $el可以获取真实dom */
    document.body.appendChild(vm.$el)
    /* 获取组件实例 */
    const comp = vm.$children[0]
    /* 添加销毁组件的方法 */
    comp.remove = function () {
      document.body.removeChild(vm.$el)
      vm.$destroy()
    }

    return comp
}

export default {
/* install方法实现的需求是 在使用组件时不需要再传入组件模板,只需传入参数,
   因此使用柯里化函数实现。*/
  install(Vue) {
    Vue.prototype.$fullLoginAd = function(options) {
      if (!instance){
        instance = create(FullLoginAd, options)
      }
      return instance
    }
  }
}

// 绑定得组件实例,通过组件实例方法控制,传递方法传参修改data里定义得内容

另一种写法:

import FullLoginAd from '@/components/tencentAir/ads/FullLoginAd.vue';

export default {
    install(Vue){
        // 生成一个Vue的子类
        // 同时这个子类也就是组件
        const FullLoginAdConstructor = Vue.extend(FullLoginAd)
        // 生成一个该子类的实例
        const instance = new FullLoginAdConstructor();

        // 将这个实例挂载在创建的div上
        // 并将此div加入全局挂载点内部
        instance.$mount(document.createElement('div'))
        document.body.appendChild(instance.$el)

        // 通过Vue的原型注册一个方法
        // 让所有实例共享这个方法 
        Vue.prototype.$showFullLoginAd = (options) => {
            instance.showFlag = true;
            if (options){
                for (let k in options){
                    instance[k] = options[k]
                }
            }
        }
        Vue.prototype.$hideFullLoginAd = () => {
            instance.showFlag = false;
        }
    }
};

// 操作组件实例属性控制,可控制props里得也可控制data里得

2. main.js使用

// 绑定到vue实例上创建登录框
import FullLoginAd from '@/components/tencentAir/ads/FullLoginAd/index.js'
Vue.use(FullLoginAd)

3. 使用即可

// 创建实例,拿到实例对象,调用对象方法show

// 可以给show方法传递参数,达到弹窗动态展示数据得目的

this.$fullLoginAd().show()

this.$fullLoginAd().hide()

参考文章:

https://www.jianshu.com/p/9423f562c130

免责声明:文章转载自《vue全局弹窗》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇kafka中的消费组虚拟内存页面文件pagefile.sys(棉文件)改变存放位置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

去掉console.log,正式环境不能有console.log

方法1 vscode中全局搜console  然后替换掉就行了(空格替换调) 方法2:webpack打包时去除,适合Vue项目 在webpack.prod.conf.js里面的plugins里面加上 drop_debugger: true, drop_console: true...

vue常见依赖安装

1):$ npm install less less-loader --save 2)style里 <style lang='less'> 2): $ npm i vue-resource --save import VueResource from 'vue-resource' Vue.use(VueResource); 就是这么简单...

[记录]解决vue项目当直接通过url访问中间页时nginx返回404的问题

    应用为VUE单页应用,路由模式为history,web服务器为nginx,正常情况下如果直接通过url访问一个中间页(不是index.html)时,会看到nginx返回的404错误,这个问题目前我只能通过修改nginx站点配置文件来实现。 具体代码(只看红色加粗的部分就行): server { listen 80; serv...

vue-awesome-swiper 配置 (分页不显示鼠标无效渐变无效等)

代码片断 import { Swiper, EffectFade, Mousewheel, Pagination } from 'swiper' import { directive } from 'vue-awesome-swiper' import 'swiper/swiper-bundle.css' Swiper.use([EffectFade,...

vite vue插件打包配置

import { defineConfig, UserConfigExport, ConfigEnv } from "vite"; import externalGlobals from "rollup-plugin-external-globals"; import vue from "@vitejs/plugin-vue"; import dts f...

Linux安装最新版Vue或者指定版本

ubuntu安装最新版Vue或者指定版本 首先已经安装了node.js 使用命令查看 $: node -v v10.17.0 再使用如下命令: npm install -g @vue/cli 安装后查看: $: vue -V @vue/cli 4.3.1 安装制定版本: #这个命令是百度的具体没有用过 npm install -g @vue/cli...