标签:vue toastRegistry extend text toastDOM Vue 组件
作用:将ui组件转为js组件
利用Vue.extend()封装一个toast组件
1、components/MyToast/index.vue
<template> <div class="container" v-if="show"> <div>{{ text }}</div> </div> </template> <script> export default { name: 'Toast' } </script> <style scoped> .container { position: fixed; top: calc(50% - 20px); left: calc(50% - 50px); width: 100px; height: 40px; text-align: center; line-height: 40px; color: #fff; background-color: rgba(0, 0, 0, 0.8); border-radius: 10px; box-sizing: border-box; } </style>
components/MyToast/index.js
import Vue from 'vue' import Toast from './index.vue' const ToastConstructor = Vue.extend(Toast) function showToast(text, duration = 2000) { const toastDOM = new ToastConstructor({ el: document.createElement('div'), data() { return { text: text, show: true } } }) document.body.appendChild(toastDOM.$el) // console.log(toastDOM) setTimeout(() => { toastDOM.show = false }, duration) } function toastRegistry() { Vue.prototype.$myToast = showToast } export default toastRegistry
2、main.js
import toastRegistry from '@/components/MyToast' Vue.use(toastRegistry)
3、使用
<button @click="$myToast('123',1500)">按钮</button>
https://www.cnblogs.com/wuqilang/p/12359571.html
标签:vue,toastRegistry,extend,text,toastDOM,Vue,组件 来源: https://www.cnblogs.com/wuqilang/p/15116496.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。