2
Hello, I have an array and the user can shuffle the order of the items:
Blusas = ['verde','azul','amarela','rosa','laranja','preta','roxa','vermelha',]
After shuffling, this is the update capture code, turning the array into a string:
window.localStorage.setItem('Blusas', JSON.stringify(this.Blusas))
And this is the code to save the update on Oninit:
this.Blusas = localStorage.getItem('Blusas');
However, I get this error flagged in "this.Blouses":
Type 'string' is not Assignable to type 'string[]'
Can anyone help me? I can’t find a solution anywhere.
is a little confused, where it gives the error, on set or get? "And this is the code to save the update on Oninit" this "saved" code I think would be "read" right? I tested the set/get in the browser and it works, wouldn’t that be a problem with this? There’s no code at all
– Ricardo Pontual
That’s right, reading code. From what I search, it’s as if "this.Blusas" is not recognized as a string and therefore cannot be stored. I tried to switch to "Blouses: string = ['green','blue','yellow','orange','black','purple','red'] but the array is still not recognized as a string.
– Mike Marins
Some suggestions to improve your code... 1. Name properties and variables in
camelCase
. I mean, renameBlusas
forblusas
. 2. Avoid accessing localStorage this way. The nicest thing would be for you to encapsulate access through a service, which would bring several advantages. Greater adherence to SRP, greater abstraction, decoupling, ease of testing, etc.– Allan Juan