溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用Vue封裝全局toast組件

發布時間:2022-10-12 14:57:39 來源:億速云 閱讀:158 作者:iii 欄目:web開發

本篇內容介紹了“怎么用Vue封裝全局toast組件”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

一. 借助 vue-cli

1. 定義 Toast 組件

// components/Toast

<template>
  <transition name="fade">
    <div v-show="visible">{{message}}</div>
  </transition>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      message: ""
    }
  }
}
</script>

<style scoped>
div {
  position: fixed;
  top: 30%;
  left: 50%;
  padding: 5px 20px;
  color: #fff;
  background-color: #424242;
  border-radius: 5px;
  text-align: center;
  transform: translate(-50%, -50%);
}
/* vue動畫過渡效果設置 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

2. 在 main.js 里面配置

import Vue from "vue"
import App from "./App.vue"
import Toast from "./components/Toast"

// 定義插件對象
const ToastObj = {
  install: function (Vue) {
    // 創建一個Vue的“子類”組件
    const ToastConstructor = Vue.extend(Toast)
    // 創建一個該子類的實例,并掛載到一個元素上
    const instance = new ToastConstructor()
    console.log(instance)
    // 將這個實例掛載到動態創建的元素上,并將元素添加到全局結構中
    instance.$mount(document.createElement("div"))
    document.body.appendChild(instance.$el)

    // 在Vue的原型鏈上注冊方法,控制組件
    Vue.prototype.$toast = (msg, duration = 1500) => {
      instance.message = msg
      instance.visible = true
      setTimeout(() => {
        instance.visible = false
      }, duration)
    }
  }
}
Vue.use(ToastObj)

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount("#app")

3. 在其他組件內使用

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => {
    return {
      msg: "HelloWord"
    }
  },
  mounted () {
   // 使用 toast 組件
    this.$toast("組件掛載成功")
  }
}
</script>

二、不借助 vue-cli

 在借助 vue-cli 的情況下,可以方便實現組件的導入導出,但是在不借助構建工具的情況下,就需要使用其他方法了

1. 注冊 toast 組件

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./static/vue/vue.min.js"></script>
</head>
<body>
  <div id="app">
    <my-button></my-button>
  </div>
  <div id="toast"></div>
  <script>
    // 定義 toast 全局組件
    const Toast = Vue.component("toast", {
      data() {
        return {
          isShow: false,
          message: "全局提示",
          wrapperStyle: {
            position: "fixed",
            top: "20%",
            left: "50%",
            zIndex: 10000,
            padding: "6px 12px",
            backgroundColor: "#424242",
            borderRadius: "5px",
            transform: "translate(-50%, -50%)"
          },
          textStyle: {
            color: "#fff",
            fontSize: "14px"
          }
        }
      },
      template: `<div v-show="isShow" :style="wrapperStyle">
                  <span :style="textStyle">{{ message }}</span>
                </div>`
    })

2. 注冊 toast 插件

// 定義插件對象
const ToastObj = {
  install: function (Vue) {
    // 創建一個 toast 組件實例,并掛載到一個元素上
    const instance = new Toast()
    // 將這個實例掛載到DOM中
    instance.$mount("#toast")

    // 在Vue的原型鏈上注冊方法,控制組件
    Vue.prototype.$toast = ({message, duration = 2000} = {}) => {
      instance.message = message
      instance.isShow = true

      setTimeout(() => {
        instance.isShow = false
      }, duration)
    }
  }
}
// 注冊 toast 插件
Vue.use(ToastObj)

3. 在其他組件內使用

Vue.component("my-button", {
      data() {
        return {
          wrapperStyle: {
            width: "70px",
            padding: "20px",
            backgroundColor: "green"
          },
          textStyle: {
            color: "#fff",
            fontSize: "16px"
          }
        }
      },
      methods: {
        handleClick() {
          this.$toast({
            message: "我點擊了"
          })
        }
      },
      template: `<div :style="wrapperStyle" @click="handleClick">
                  <span :style="textStyle">點擊提示</span>
                </div>`
    })

    const vm = new Vue({
      el: "#app"
    })
  </script>
</body>
</html>

“怎么用Vue封裝全局toast組件”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女