美文网首页
VUE手动实现一个编程式组件

VUE手动实现一个编程式组件

作者: 帅的潇洒 | 来源:发表于2020-04-17 14:34 被阅读0次

1.创建一个组件(用于编程式调用的)

<template>
  <div class='toast'
       v-show='isShow'>
    {{message}}
  </div>
</template>
<script>
export default {
  data () {
    return {
      message: '',
      isShow: false
    }
  },
  methods: {
    show (message, duration) {
      this.message = message
      this.isShow = true
      setTimeout(() => {
        this.isShow = false
        this.message = ''
      }, duration)
    }
  }
}
</script>
<style scoped>
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 999;
  padding: 8px 10px;
  background-color: rgba(0, 0, 0, 0.3);
}
</style>

2.实现编程式

import Toast from './toast'
const obj = {}
obj.install = function (Vue) {
  // 创建构造器
  const ToastContrystor = Vue.extend(Toast)
  // new的方式 根据组件构造器,可以创建组件对象
  const toast = new ToastContrystor()
  // 手动挂载某一个元素上
  toast.$mount(document.createElement('div'))
  // toast.$el对应的就是div
  document.body.appendChild(toast.$el)
    //组件挂载到Vue原型上
  Vue.prototype.$toast = toast
}
export default obj

3.在main.js注册

import Vue from 'vue'
import toast from '@/components/common/toast/index.js'
Vue.use(toast)

4.使用

this.$toast.show('这不是一个错误',1000)

相关文章

  • (24)打鸡儿教你Vue.js

    学习Vue基础语法Vue中的组件Vue-cli的使用 1、使用Vue2.0版本实现响应式编程2、理解Vue编程理念...

  • VUE手动实现一个编程式组件

    1.创建一个组件(用于编程式调用的) 2.实现编程式 3.在main.js注册 4.使用

  • 【Vue3.0】- 响应式

    响应式原理 响应式是 Vue.js 组件化更新渲染的一个核心机制 Vue2.x响应式实现 Object.defin...

  • Vue 实现手动刷新组件

    开发过程遇到了一个问题,就是我的 router-view 里面渲染出来的组件输入数据之后,我点击 路由视图外边的导...

  • Vue的特点

    vue两大特点:响应式编程、组件化。 vue的优势:轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、...

  • Vue系列(一) -- 基础知识

    概述 Vue 的特点是: 响应式编程:保持状态和视图的同步(数据绑定) 组件化:一切都是组件 Vue 只关注 da...

  • vue params、query传参使用

    声明式:编程式:router.push(...)主要实现:通过A组件...

  • 学习搭建Vue的TypeScript环境

    实现目标:搭建 Vue 的 TS 版编程环境,并学习下 Vue 的类组件使用 一、配置 webpack 首先通过:...

  • Vue磨刀嚯嚯

    Vue开发风格——传统方法应用vue.js Vue开发风格——单个组件式 组件 基本操作 创建一个组件构造器 注册...

  • 手写系列之 —— 实现Spring事务注解功能

    Spring事务分为编程式事务和声明式事务,编程式事务是手动控制,声明式事务是利用注解或者配置文件自动实现事务控制...

网友评论

      本文标题:VUE手动实现一个编程式组件

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