如何通过将模块名称作为字符串从商店访问模块?

时间:2021-01-06 07:31:54

标签: vue.js vuex

我是 Vuejs 的新手,遇到了问题。

我需要使用商店,但我只有字符串名称。我知道我可以通过编写来访问我编写的商店方法 (all() ): storeName.all

但我只有 this.storeName 作为字符串并写入 this.storeName.all 不起作用

2 个答案:

答案 0 :(得分:0)

如果您想在模板中访问

<template>

  {{$store.state.<varibale>}}
<template>

在JS中

this.$store.state

这里是 Sandbox 链接,可帮助您设置和使用它。

https://codesandbox.io/s/silly-poitras-sosvs?file=/src/App.vue

答案 1 :(得分:0)

这家店基本上是个下贱的。我们可以通过以下方式访问对象属性:

this.$store.storeName;

或者如果我们知道字符串中的商店名称,我们可以这样访问:

this.$store["storeName"];