日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vuetify table_vuex 封装设计全局可用的vuetify中的snackbar

發布時間:2025/4/5 vue 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vuetify table_vuex 封装设计全局可用的vuetify中的snackbar 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Vuetify是 Vue 的一個UI框架,幫助我們快速搭建應用。有眾多優秀的組件可供選擇:

  • v-data-table 顯示表格數據
  • v-dialog 實現彈框設計
  • v-toolbar等實現橫向菜單
  • ...

而Vuetify中的 snackbar 類似element 中的message消息提示,實現 成功、警告、消息等消息反饋。

The world's most popular Vue UI framework?element.eleme.cn

Element 為 Vue.prototype 添加了全局方法 $message。因此在 vue instance 中可以采用本頁面中的方式調用Message。

import { Message } from 'element-ui';

引入之后就可以調用 Message 諸如 .success .closeAll等各種方法來彈出或關閉消息提示。

而vuetify的snackbar 則需要自行設置:

將vuetify snackbar進行封裝,用vuex 來傳遞消息內容。

建立 store/modules/snackbar.js,管理 snackbar 的變量。

/*** @param msg 信息* @param color snackbar 顏色* @param visible 是否可見* @param showClose 關閉按鈕* @param timeout 停留持續時間 */ const snackbar = {// namespaced: true,state: {msg: '',color: '',visible: false,showClose: true,timeout: 5000,}, // 邏輯處理,同步函數mutations: {OPEN_SNACKBAR(state, options) {state.visible = truestate.msg = options.msgstate.color = options.color},CLOSE_SNACKBAR(state) {state.visible = false},setShowClose(state, isShow) {state.showClose = isShow},setTimeout(state, timeout) {state.timeout = timeout},},// 邏輯處理,異步函數actions :{openSnackbar (context,options){let timeout = context.state.timeoutcontext.commit('OPEN_SNACKBAR',{msg:options.msg,color:options.color})setTimeout(()=>{context.commit('CLOSE_SNACKBAR')},timeout)}} } export default snackbar;

在vuex的index 中引入這個模塊:

import Vue from "vue"; import Vuex from "vuex"; // 放置全局信息 Vue.use(Vuex);import snackbar from "@/store/modules/snackbar"; export default new Vuex.Store({modules: {snackbar,}, });

再在components中建立snackbar組件:

<template><v-snackbar top text v-model="visible" :color="color">{{ this.$store.state.snackbar.msg }}<!-- 關閉按鈕 --><template v-slot:action="{ attrs }" ><v-btn v-bind="attrs" v-if="showClose" icon @click="close" :color="color"><v-icon>mdi-close</v-icon></v-btn></template></v-snackbar> </template> <script> export default {data() {return {};},computed: {visible() {return this.$store.state.snackbar.visible;},showClose() {return this.$store.state.snackbar.showClose;},color() {return this.$store.state.snackbar.color;},},methods: {close() {this.$store.commit("snackbar/CLOSE_SNACKBAR");},}, }; </script>

最后在App.vue中引入snackbar組件

<template><div id="app"><v-app><router-view /><Snackbar /></v-app></div> </template> <script> import Snackbar from '@/components/_partial/Snackbar.vue' export default {components:{Snackbar}, } </script>

由于vuex的全局屬性,所以你可以在頁面的任何地方調用dispatch來創建消息條的實例:

methods:{createdSnackbar(){this.$store.dispatch('snackbar/openSnackbar',{msg:'瓦拉伊利五路',color:'primary'})}}

總結

以上是生活随笔為你收集整理的vuetify table_vuex 封装设计全局可用的vuetify中的snackbar的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。