Vuetify Snackbar在x秒后消失,如何防止

时间:2018-12-08 18:11:43

标签: vue.js vuetify.js

以某种方式,我试图在6秒钟后消失的Snackbar消失了,我试图阻止这种情况,但我失败了。这就是我的Vue组件的外观,基本上就像是带有其他Button的Snackbar。

<template>
    <div>
        <v-snackbar
            :timeout="timeout"
            :bottom="'bottom'"
            :right="'right'"
            :auto-height="true"
            v-model="googleAnalyticsSnackbar"
        >
            {{ text }}
             <v-btn flat color="pink">
                 <a href="#">test</a>
            </v-btn>
            <v-btn flat color="pink" @click.native="googleAnalyticsSnackbar = false">yes</v-btn>
            <v-btn flat color="pink" @click.native="googleAnalyticsSnackbar = false">no</v-btn>
        </v-snackbar>
    </div>      
</template>

<script>
    export default {

        mounted() {
            if(this.checkCookieStatus() == false || this.checkCookieStatus() == null) {
                this.googleAnalyticsSnackbar = true;
            }
        },

        data: () => ({
            googleAnalyticsSnackbar: true,
            timeout: 0,
            text: 'foo'
        }),

        methods: {
            acceptCookie() {
                this.$cookie.set('cookie_accept_status', true, 1);
            },

            declineCookie() {
                this.$cookie.set('cookie_accept_status', false, 1);
            },

            checkCookieStatus() {
                return this.$cookie.get('cookie_accept_status');
            }
        }

    };
</script>

我在主js文件中这样称呼它:

Vue.component('google-analytics-cookie', require('./components/GoogleAnalyticsCookieToastComponent.vue'));

并将其像这样包含在我的html中:

        <google-analytics-cookie></google-analytics-cookie>

但是Snackbar在大约6秒钟后消失了,文档说0应该阻止它发生,但是不会起作用。

3 个答案:

答案 0 :(得分:3)

问题在于底部和右侧属性绑定:

:bottom="'bottom'"
:right="'right'"

应为true或false,而不是字符串。

它应该像文档中描述的那样工作。示例:

new Vue({
  el: '#app',
  data () {
    return {
      snackbar: true,
      timeout: 0,
      bottom: true,
      right: true
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.3.12/vuetify.min.js" integrity="sha256-MDwft2SRPd5GCJGWRIbkS5l7Gcr3S3vJr7MKpe1Z3+Q=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.3.12/vuetify.min.css" integrity="sha256-h8aOnZm3FeYz5m4z7eI3xc0uJX3l/mF+0i2643O7ieM=" crossorigin="anonymous" />

<div id="app">
  <v-app>
    <v-snackbar :timeout="timeout" v-model="snackbar" :bottom="bottom" :right="right" color="indigo">Hi There <v-btn @click="snackbar=false" dark>Close</v-btn></v-snackbar>
  </v-app>
</div>

答案 1 :(得分:0)

new Vue({
  el: '#app',
  data () {
    return {
      snackbar: true,
      timeout: 0,
      bottom: true,
      right: true
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.3.12/vuetify.min.js" integrity="sha256-MDwft2SRPd5GCJGWRIbkS5l7Gcr3S3vJr7MKpe1Z3+Q=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.3.12/vuetify.min.css" integrity="sha256-h8aOnZm3FeYz5m4z7eI3xc0uJX3l/mF+0i2643O7ieM=" crossorigin="anonymous" />

<div id="app">
  <v-app>
    <v-snackbar :timeout="timeout" v-model="snackbar" :bottom="bottom" :right="right" color="indigo">Hi There <v-btn @click="snackbar=false" dark>Close</v-btn></v-snackbar>
  </v-app>
</div>

答案 2 :(得分:0)

为了防止自动关闭小吃店,请将超时设置为 -1。 当我将超时设置为 0 时,控制台警告说它已弃用并建议将其设置为 -1