【问题标题】:How do I use Vuex State data in methods如何在方法中使用 Vuex 状态数据
【发布时间】:2019-05-23 17:52:48
【问题描述】:

使用以下代码

<template>
  <div :id="svgId" class="svg-container"></div>
</template>
<script>
import { mapState } from 'vuex'

export default {
  name: 'Space',

  data: function() {
    return {
      svgId: 'space',
      svgContainer: null
    }
  },

  computed: mapState({
    notes: state => state.notes
  }),

  mounted: function() {
    this.generateSpace()
  },
  methods: {
    generateSpace: function() {
      this.$svg('space')
        .rect(100, 100)
        .attr({ fill: '#f06' })
    }
  }
}
</script>

我目前只画一个粉红色的正方形,但我想做的是在方法生成空间中使用来自商店的数据,但我不确定如何访问它,例如这样的东西;

 this.$svg('space')
        .rect(notes.height, notes.width)
        .attr({ fill: notes.color })
    }

感谢任何提示。

【问题讨论】:

    标签: vue.js vuex svg.js


    【解决方案1】:

    我认为只需将this 添加到notes 就足够了。

     this.$svg('space')
            .rect(this.notes.height, this.notes.width)
            .attr({ fill: this.notes.color })
        }
    

    【讨论】:

    • 啊哈,当然!我现在的问题是第一次加载数据还没有准备好?如果我像魅力一样编辑作品,一旦加载页面,它是未定义的?那么我什么时候应该调用我的函数,因为挂载太早了?
    • 很高兴它有帮助 :) 通常只有在组件中的所有内容都设置好时才会触发安装的钩子。您是否尝试设置一些毫秒的 setTimeout 来检查问题是否真的与调用挂载时间有关?因为也许问题是数据没有设置。
    • 呃,不,会有戏
    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2019-06-11
    • 1970-01-01
    • 2023-03-20
    • 2020-05-18
    • 2018-08-06
    相关资源
    最近更新 更多