How to add localstorage in Typescript (Angular 6)

Asked

Viewed 3,027 times

0

I have a code in Typescript (angular 6) in which I for educational reasons, want to know how to save various message variables with the following data in cache:

adicionarMensagem(preparacao: PreparacaoDeMensagem) {
    let mensagem = {
        texto: preparacao.texto,
        data: preparacao.data,
        contato: preparacao.contato,
        souEumesmo: preparacao.contato === this.emissor,
}

This is the function I use to add a message, but I also want to save it in the browser cache.

  • I commented on how to use localStorage and sessionStorage in this answer: https://answall.com/a/387053/5407

1 answer

1

In Brower, among other modes of Storage there is the localstorage that can be accessed via window.localstorage

For example within the function that showed would just call the function window.localstorage.setItem(‘menssagem’, JSON.stringify(menssagem)) that your object will be persisted.

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.