Vue Js Components不被销毁吗?

时间:2018-07-12 12:59:08

标签: vue.js vuejs2 vue-component nuxt.js

我目前正在使用非常通用的仪表板来显示各种数据。

对于前端,我使用的是最新的nuxt和vue版本。

我的仪表板具有多种显示数据的变体(例如饼图,折线图等),这些变体在动态调用的组件中进行了描述。

问题是当我从页面“ /”浏览到另一个页面(例如“ / foo”)时,间隔再次被触发并导致应用崩溃。 这是在生命周期挂钩被破坏之后发生的。我试图将时间间隔定义为变量,并在beforeDestroy挂钩中将其停止,但这没有帮助。

let interval= setInterval(this.fetchData.bind(null, configuration, connector), configuration.refreshTime)

/* later */
clearInterval(interval);

看到错误了吗?

谢谢。


那是相关代码:

模板

<no-ssr>
  <v-container grid-list-md>
    <v-layout row wrap v-masonry transition-duration="0.5s" item-selector=".flex" column-width="#grid-sizer">
      <v-flex xs1 sm1 md1 lg1 x1 id="grid-sizer"></v-flex>
      <component :key="dashboardItem.id" v-for="(dashboardItem,index) in dashboardItems" :is="dashboardItem.type" :connector="dashboardItem.connector"
        :type="dashboardItem.type" :configuration="dashboardItem.configuration" :id="dashboardItem.id" :index="index"></component>
    </v-layout>
  </v-container>
</no-ssr>

脚本

import OnlyValue from '@/components/dashboardItems/OnlyValue.vue'
import TrafficLight from '@/components/dashboardItems/TrafficLight.vue'
import ChartAllHover from '@/components/dashboardItems/ChartAllHover.vue'
import PieChart from '@/components/dashboardItems/PieChart.vue'
import Section from '@/components/dashboardItems/Section.vue'
import Gauge from '@/components/dashboardItems/Gauge.vue'...    
export default {
  name: 'HomePage',

  head () {
    return {
      title: "Dashboard"
    }
  },

  computed: {
    ...mapGetters({
      isAuthenticated: "users/isAuthentificated",
      profileName: "profiles/name",
      dashboardItems: "profiles/dashboardItems"
    })
  },

  mounted() {
    if (typeof this.$redrawVueMasonry === 'function') {
      this.$redrawVueMasonry()
    }
  },

  components: {
    OnlyValue,
    TrafficLight,
    ChartAllHover,
    PieChart,
    Section,
    Gauge
  }
}

调用组件时,其外观如下:

import dashboardItem from '~/mixins/dashboardItem'

export default {
name: "gauge",

mixins: [dashboardItem],

props: {
    connector: {
      type: String,
      required: true
    },
    type: {
        type: String,
        required: true
    },
    configuration: {
        type: Object,
        required: true
    },
    id: {
        type: Number,
        required: true
    },
    index: {
        type: Number,
        required: true
    }
  },

  data: () => ({
    initOptions: {
        renderer: 'svg'
    },
    options: {
      tooltip: {
        formatter: "{c}%"
      },
      series: [{
        name: null,
        type: 'gauge',
        detail: {
          formatter: '{value}%'
        },
        data: null
      }]
    },
    isLoading: true
  }),

  methods: {
    getData(configuration, connector) {
      this.fetchData(configuration, connector)
      setInterval(this.fetchData.bind(null, configuration, connector), configuration.refreshTime)
    },
    fetchData(configuration, connector) {
      this.getSingleValue(configuration, connector)
        .then(data => {
          this.isLoading = false
          let percent = (data.value / configuration.max) * 100
          percent = Math.round(percent * 10) / 10
          this.$nextTick(function () {
            this.$refs.gauge.mergeOptions({
              series: [{
                name: data.title,
                data: [{
                  value: percent,
                  name: data.title
                }]
              }]
            })
          })
          this.$redrawVueMasonry()
        })
        .catch(e => console.log(e))
    }
  },

  mounted () {
    this.getData(this.configuration, this.connector)
  }
}

0 个答案:

没有答案