如何将Vue消息提示全局组件改写为长尾?

2026-04-09 23:151阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计518个文字,预计阅读时间需要3分钟。

如何将Vue消息提示全局组件改写为长尾?

简介:简述项目的基本情况。实现功能:描述项目实现的主要功能。自定义文本:添加项目特有的文本描述。自定义类型(默认,消息,成功,警告,危险):定义组件的类型,包括默认、消息、成功、警告和危险。自定义过渡时间:设置组件的过渡效果时间。使用vue-cli3.0生成项目:说明项目是基于vue-cli3.0创建的。toast全局组件编写:介绍如何编写toast全局组件。/src/toast/toast.vue:

如何将Vue消息提示全局组件改写为长尾?

简介

实现功能

  • 自定义文本
  • 自定义类型(默认,消息,成功,警告,危险)
  • 自定义过渡时间

使用vue-cli3.0生成项目

toast全局组件编写

/src/toast/toast.vue

<template> <div class="app-toast" v-if="isShow" :class="{'info': type=== 'info','success': type=== 'success','wraning': type=== 'wraning','danger': type=== 'danger'}">{{text}}</div> </template> <style scoped> .app-toast { position: fixed; left: 50%; top: 50%; background: #ccc; padding: 10px; border-radius: 5px; transform: translate(-50%, -50%); color: #fff; } .info { background: #00aaee; } .success { background: #00ee6b; } .wraning { background: #eea300; } .danger { background: #ee000c; } </style>

/src/toast/index.js

import vue from 'vue' import toastComponent from './toast.vue' // 组件构造器,构造出一个 vue组件实例 const ToastConstructor = vue.extend(toastComponent) function showToast ({ text, type, duration = 2000 }) { const toastDom = new ToastConstructor({ el: document.createElement('div'), data () { return { isShow: true, // 是否显示 text: text, // 文本内容 type: type // 类型 } } }) // 添加节点 document.body.appendChild(toastDom.$el) // 过渡时间 setTimeout(() => { toastDom.isShow = false }, duration) } // 全局注册 function registryToast () { vue.prototype.$toast = showToast } export default registryToast

全局注册

/main.js

import toastRegistry from './toast/index' Vue.use(toastRegistry)

调用

/src/views/home.vue

<template> <div class="home"> <input type="button" value="显示弹窗" @click="showToast"> </div> </template> <script> export default { name: 'home', methods: { showToast () { this.$toast({ text: '我是消息' // type: 'wraning', // duration: 3000 }) } } } </script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计518个文字,预计阅读时间需要3分钟。

如何将Vue消息提示全局组件改写为长尾?

简介:简述项目的基本情况。实现功能:描述项目实现的主要功能。自定义文本:添加项目特有的文本描述。自定义类型(默认,消息,成功,警告,危险):定义组件的类型,包括默认、消息、成功、警告和危险。自定义过渡时间:设置组件的过渡效果时间。使用vue-cli3.0生成项目:说明项目是基于vue-cli3.0创建的。toast全局组件编写:介绍如何编写toast全局组件。/src/toast/toast.vue:

如何将Vue消息提示全局组件改写为长尾?

简介

实现功能

  • 自定义文本
  • 自定义类型(默认,消息,成功,警告,危险)
  • 自定义过渡时间

使用vue-cli3.0生成项目

toast全局组件编写

/src/toast/toast.vue

<template> <div class="app-toast" v-if="isShow" :class="{'info': type=== 'info','success': type=== 'success','wraning': type=== 'wraning','danger': type=== 'danger'}">{{text}}</div> </template> <style scoped> .app-toast { position: fixed; left: 50%; top: 50%; background: #ccc; padding: 10px; border-radius: 5px; transform: translate(-50%, -50%); color: #fff; } .info { background: #00aaee; } .success { background: #00ee6b; } .wraning { background: #eea300; } .danger { background: #ee000c; } </style>

/src/toast/index.js

import vue from 'vue' import toastComponent from './toast.vue' // 组件构造器,构造出一个 vue组件实例 const ToastConstructor = vue.extend(toastComponent) function showToast ({ text, type, duration = 2000 }) { const toastDom = new ToastConstructor({ el: document.createElement('div'), data () { return { isShow: true, // 是否显示 text: text, // 文本内容 type: type // 类型 } } }) // 添加节点 document.body.appendChild(toastDom.$el) // 过渡时间 setTimeout(() => { toastDom.isShow = false }, duration) } // 全局注册 function registryToast () { vue.prototype.$toast = showToast } export default registryToast

全局注册

/main.js

import toastRegistry from './toast/index' Vue.use(toastRegistry)

调用

/src/views/home.vue

<template> <div class="home"> <input type="button" value="显示弹窗" @click="showToast"> </div> </template> <script> export default { name: 'home', methods: { showToast () { this.$toast({ text: '我是消息' // type: 'wraning', // duration: 3000 }) } } } </script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。