美文网首页Vue学习
手动挂载组件-demo

手动挂载组件-demo

作者: 冰滩波纹 | 来源:发表于2020-09-18 14:25 被阅读0次
  1. MyNotice/MyNotice.vue
<template>
    <div class="notice-box">
        <div class="message" 
            :class="item.type" 
            v-for="item in msgList" 
            :key="item._name"
        >
            <div class="content">{{item.content}}</div>
        </div>
    </div>
</template>
<script>
const DefaultOption = {
    duration: 1500,
    type: "normal",
    content: "提示内容!",
};
let idx = 0;
export default {
    data() {
        return {
            msgList: [],
        };
    },
    methods: {
        add(msgOpt = {}) {
            // name标识 用于移除弹窗
            let _name = this.getName();
            // 并入默认配置,并赋值
            msgOpt = Object.assign(
                {
                    _name,
                },
                DefaultOption,
                msgOpt
            );
            this.msgList.push(msgOpt);

            setTimeout(() => {
                this.removeMsg(_name);
            }, msgOpt.duration);
        },
        getName() {
            return "msg_" + idx++;
        },
        removeMsg(_name) {
            let index = this.msgList.findIndex((item) => item._name === _name);
            this.msgList.splice(index, 1);
        },
    },
};
</script>
<style lang='scss' scoped>
.notice-box {
    position: fixed;
    top: 50px;
    left: 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
    transform: translateX(-50%);

    .message {
        border-width: 3px;
        min-width: 240px;
        max-width: 500px;
        margin-bottom: 10px;
        border-radius: 3px;
        box-shadow: 0 0 8px #ddd;
        overflow: hidden;

        &.normal {
            border-left: 3px solid #909399;
            background: #f4f4f5;
        }
        &.success {
            border-left: 3px solid #67c23a;
            background: #f0f9eb;
        }
        &.error {
            border-left: 3px solid #f56c6c;
            background: #fef0f0;
        }
        &.warning {
            border-left: 3px solid #e6a23c;
            background: #fdf6ec;
        }

        .content {
            padding: 8px;
            line-height: 1.3;
        }
    }
}
</style>
  1. MyNotice/index.js
import Vue from 'vue'
import MyNotice from './MyNotice.vue'

let noticInstance = null,
    NoticeConstructor = Vue.extend(MyNotice),
    init = () => {
        noticInstance = new NoticeConstructor()
        noticInstance.$mount()
        document.body.appendChild(noticInstance.$el)
    },
    caller = (opt) => {
        if (!noticInstance) {
            init()
        }
        noticInstance.add(opt)
    }

export default {
    // 返回 install 函数 用于 Vue.use 注册
    install(vue) {
        vue.prototype.$notice = caller
    },
}
  1. main.js
import MyNotice from '@/components/MyNotice/index.js'

Vue.use(MyNotice)
  1. 调用
 myNotice() {
    this.$notice({
        type: "success",
        content: "成功信息提示",
        duration: 2000,
    })
 }

相关文章

  • 手动挂载组件-demo

    官方文档:https://cn.vuejs.org/v2/api/#vm-mount 仿写 elementUI的 ...

  • vue手动挂载组件

    在一些需求中,手动挂载组件能够让我们实现起来更加优雅。比如一个弹窗组件,最理想的用法是通过命令式调用,就像 ele...

  • Vue 的⽗组件和⼦组件⽣命周期钩⼦执⾏顺序是什么?

    渲染过程:⽗组件挂载完成⼀定是等⼦组件都挂载完成后,才算是⽗组件挂载完,所以⽗组件的mounted在⼦组 件mou...

  • Ubuntu手动挂载共享文件夹

    VirtualBox 手动挂载共享文件夹 安装增强 1、界面执行安装增强 2、挂载镜像 3、执行安装增强功能 手动...

  • linux 开机自动挂载磁盘

    查看磁盘信息 在要挂载到的地方新建文件夹,比如 设置开机挂载 手动挂载: 自动挂载: 立即生效

  • vue组件生命周期挂载顺序

    title: vue组件挂载顺序date: 2016-12-02 vue组件挂载顺序 本文通过实验介绍vue组件的...

  • armbian挂载硬盘

    手动挂载 检测磁盘 建立目录 挂载 开机自动挂载 查看磁盘信息 记下UUID、TYPE 修改 /etc/fstab...

  • Vue动态组件

    多个组件使用同一个挂载点,动态切换动态组件就是将几个组件放在一个挂载点下,这个挂载点就是标签...

  • 生命周期

    组件将要挂载时触发的函数:componentWillMount组件挂载完成时触发的函数:componentDidM...

  • 1-5 函数式组件

    demo1 - 函数式组件 demo2 - 类组件 demo3 - 函数式组件传参 组件里面套组件 - 复合组件 ...

网友评论

    本文标题:手动挂载组件-demo

    本文链接:https://www.haomeiwen.com/subject/kkxjyktx.html