欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue組件教程之Toast(Vue.extend 方式)詳解

 更新時(shí)間:2019年01月27日 11:06:27   作者:similar  
這篇文章主要給大家介紹了關(guān)于Vue組件教程之Toast(Vue.extend 方式)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、效果圖

二、說明

這類提示框組件我們通常都會(huì)直接在 JS 代碼中進(jìn)行調(diào)用。像下面這樣:

this.$toast('別點(diǎn)啦,到頭啦!')

但看到網(wǎng)上大多數(shù)還是通過 component 方式實(shí)現(xiàn)的,這樣的話我們?cè)谑褂玫臅r(shí)候還要在 DOM 中放置一個(gè)組件元素,然后通過一個(gè)變量來控制它的顯示隱藏,這樣使用起來非常的不方便。那么有什么方法可以不用在 DOM 中手動(dòng)放置組件元素就可以直接調(diào)用呢?答案就是 Vue.extend。通過 Vue.extend 方式實(shí)現(xiàn)的組件,需要兩個(gè)文件,一個(gè)是 .vue 文件,另外一個(gè)就是管理 .vue 的 .js 文件。具體代碼如下:

三、代碼

Toast.vue 文件代碼

<template>
 <div class="toast" v-show="visible">
 {{ message }}
 </div>
</template>

<script>
export default {
 name: 'toast',
 data () {
 return {
  message: '', // 消息文字
  duration: 3000, // 顯示時(shí)長,毫秒
  closed: false, // 用來判斷消息框是否關(guān)閉
  timer: null, // 計(jì)時(shí)器
  visible: false // 是否顯示
 }
 },
 mounted () {
 this.startTimer()
 },
 watch: {
 closed (newVal) {
  if (newVal) {
  this.visible = false
  this.destroyElement()
  }
 }
 },
 methods: {
 destroyElement () {
  this.$destroy()
  this.$el.parentNode.removeChild(this.$el)
 },
 startTimer () {
  this.timer = setTimeout(() => {
  if (!this.closed) {
   this.closed = true
   clearTimeout(this.timer)
  }
  }, this.duration)
 }
 }
}
</script>

<style lang="scss" scoped>
.toast {
 position: fixed;
 bottom: 15vh;
 left: 28vw;
 min-width: 40vw;
 max-width: 100vw;
 font-size: 26px;
 text-align: center;
 padding: 10px 2vw;
 border-radius: 40px;
 background-color: rgba(0, 0, 0 , 0.6);
 color: rgb(223, 219, 219);
 letter-spacing: 3px;
}
</style>

Toast.js 文件代碼

import Vue from 'vue'
import Toast from '@/components/layer/Toast.vue'

let ToastConstructor = Vue.extend(Toast) // 構(gòu)造函數(shù)
let instance // 實(shí)例對(duì)象
let seed = 1 // 計(jì)數(shù)

const ToastDialog = (options = {}) => {
 if (typeof options === 'string') {
 options = {
  message: options
 }
 }
 let id = `toast_${seed++}`
 instance = new ToastConstructor({
 data: options
 })
 instance.id = id
 instance.vm = instance.$mount()
 document.body.appendChild(instance.vm.$el)
 instance.vm.visible = true
 instance.dom = instance.vm.$el
 instance.dom.style.zIndex = 999 + seed
 return instance.vm
}

export default ToastDialog

四、使用

首先在 main.js 中引入 Toast.js 并掛載到vue原型上,如下圖:

其次,在代碼中使用

this.$toast('別點(diǎn)啦,到頭啦!')

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論