user3469203
user3469203

Reputation: 547

Get Vuejs Store variable by String Value

I'm trying to get a vuejs store variable like this :

const pathFile = `#pathFile_${this.devisDTO.code_produit}`; const pathApp = this.$store.state.parameters.urls.${pathFile};

So in the second line ${pathFile} is not interpreted in that way. Please could you help on how to write this ?

Upvotes: 2

Views: 823

Answers (4)

Samir Lakhani
Samir Lakhani

Reputation: 743

you need to modify your code remove brackets from parameters.urls.${pathFile} to .urls.pathFile;

here is code..

const pathFile = `#pathFile_${this.devisDTO.code_produit}`;
const pathApp = this.$store.state.parameters.urls.pathFile;

Upvotes: 1

Liuka
Liuka

Reputation: 309

In JavaScript ${string_name} is used inside template strings (1). If you want to access the value of a dictionary based on a string's content you should use the square brackets syntax. In your case

this.$store.state.url[path_file]

On a side note I suggest you to use store getters to access variables.

(1): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Upvotes: 4

Nino Filiu
Nino Filiu

Reputation: 18473

That's not valid javascript. I am guessing you meant to write the following:

const pathFile = `#pathFile_${this.devisDTO.code_produit}`;
const pathApp = this.$store.state.parameters.urls[pathFile];

Upvotes: 0

Ronak Dhoot
Ronak Dhoot

Reputation: 2321

pathFile is a normal variable. Remove the brackets from it.

const pathFile = `#pathFile_${this.devisDTO.code_produit}`;
const pathApp = this.$store.state.parameters.urls.pathFile;

Upvotes: 2

Related Questions