Vue.extend 编写 Toast
2022/2/9 23:15:40
本文主要是介绍Vue.extend 编写 Toast,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
home.vue
<template> <div> <el-button @click="toToast"> 显示 </el-button> </div> </template> <script> import { showToast } from "./toast.js"; export default { methods: { toToast() { showToast("测试", 1500); }, }, }; </script> <style lang="scss" scoped> </style>
toast.js
import Vue from "vue"; import Toast from './toast.vue' const ToastCtor=Vue.extend(Toast) export function showToast(content,delay){ const toastComp=new ToastCtor().$mount() document.body.appendChild(toastComp.$el) toastComp.toShow(content) setTimeout(() => { toastComp.toClose() toastComp.$el.parentNode.removeChild(toastComp.$el) }, delay); }
toast.vue
<template> <!-- <div class="toast" v-if="show">{{message}}</div> --> <div class="toast" v-show="show">{{message}}</div> </template> <script> export default { data() { return { show: false, message:'', }; }, methods: { toShow(content) { this.message=content this.show = true; }, toClose() { this.show = false; }, }, }; </script> <style lang="scss" scoped> .toast { position: fixed; top: 10px; left: 50%; padding: 10px 20px; background: rgba(22, 21, 21, 0.658); color: #fff; font-size: 14px; border-radius: 6px; min-width: 80px; text-align: center; } </style>
这篇关于Vue.extend 编写 Toast的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set
- 2024-03-01react native uuid
- 2024-02-29vuejs sidebar