美文网首页
在vue2中函数式调用组件

在vue2中函数式调用组件

作者: 洪锦一 | 来源:发表于2022-08-05 16:47 被阅读0次

创建message目录下

image.png

index.vue

<template>
  <div class="box">
    <div :class="['box-main ', { active: isShow }]">{{ info }}==={{shijain}}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      info: "",
      shijain:'',
      isShow: false,
    };
  },
  mounted() {
    setTimeout(() => {
      this.isShow = true;
    }, 2000);
  },
  methods: {},
};
</script>

<style scoped>
.box {
  width: 100%;
  height: 100%;
  position: relative;
}
.box-main {
  width: 150px;
  min-height: 50px;
  background: #ccc;
  text-align: center;
  line-height: 50px;
  border-radius: 10px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.active {
  transition: all 1s;
  opacity: 0;
}
</style>

index.js

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

let MessageConstructor = Vue.extend(Message)
let instance
const MessageTip = function (options = {}) {
    instance = new MessageConstructor({
        data: options
    })
    document.body.appendChild(instance.$mount().$el) //this.$el拿到组件实际上的dom,把他挂载到body上
    setTimeout(() => {
        document.body.removeChild(instance.$mount().$el)
    }, 3000);  //3秒之后自动移除dom组件

}

export default MessageTip

在main.js 全局挂载

import MessageTip from '@/components/message/index.js'
Vue.prototype.$MessageTip = MessageTip

页面使用

<template>
  <div id="app">
    <button @click="showdialog">显示弹框1</button>
    <button @click="showdialog2">显示弹框2</button>
    <button @click="showdialog3">显示弹框3</button>
  </div>
</template>
<script>
export default {
  methods:{
    showdialog(){
      this.$MessageTip({info:'哈哈',shijain:new Date()})
    },
    showdialog2(){
      this.$MessageTip({info:'哈哈222'})
    },
    showdialog3(){
      this.$MessageTip({info:'哈哈333'})
    }
  }
}
<script>

参考:
https://www.cnblogs.com/zienlove/p/16186758.html
https://blog.csdn.net/HockJerry/article/details/115750572

相关文章

  • 在vue2中函数式调用组件

    创建message目录下 index.vue index.js 在main.js 全局挂载 页面使用 参考:htt...

  • Render渲染函数和JSX

    render函数 h( 元素,属性,值 ) 中 h 不能少 使用 list组件中调用 函数式组件 定义函数式组件 ...

  • React入门(二)

    组件 1.函数式组件 什么是函数式组件创建一个函数,只要函数中返回一个新的JSX元素,则为函数式组件 调用组件可以...

  • 2-useState

    Time: 20200126 函数组件中使用状态。 类组件写法 注意类组件的用法。 函数式组件写法 在事件调用中,...

  • antd-Modal.confirm

    前言 在提示交互的情况下,函数式调用比组件式调用逻辑更清晰,代码也更简洁,如antd中,modal.confirm...

  • 来给regular组件写注释吧

    在基本完全组件化的前端工程中,调用组件就像调用函数一样常见: 调用函数时,我们传入参数,得到计算结果; 调用组件时...

  • React中父组件访问函数式子组件内部属性的方法

    在之前的react开发中,我们可以直接通过ref访问类组件中的属性或调用类组件的成员方法。但是如果子组件是函数式组...

  • 笔记-React-Hooks

    一、矛与盾的问题?(Class组件与函数式组件)   在 React 中 Class 组件好用还是函数式组件好用呢...

  • React Native开发小结

    1、函数式组件 函数式组件即通过调用一个方法返回一个组件,可以解决组件开发时嵌套过多组件代码臃肿问题。 将一个组件...

  • vue 中使用 jsx 总结

    vue 中使用 jsx 调用方式 标签函数组件// 模式1: 类式函数组件const Sub = { fun...

网友评论

      本文标题:在vue2中函数式调用组件

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