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

How to access a module from a store by having its name as a string?

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

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

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

如果要在模板中访问

<template>

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

在 JS 中

this.$store.state

这是沙盒 link 将帮助您设置和使用它。

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

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

this.$store.storeName;

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

this.$store["storeName"];