1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

sessionStorageの詳しい説明はこちら

セッションストレージを使用することで、URLパラメータを使用せずに遷移先のページに値を受け渡すことができます。入力データの一時保存にも使用できます。

セッションストレージにデータを保存する

sessionStorage.setItem("key", "value");を使用します。

import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class SampleSetSessionStorage extends NavigationMixin(LightningElement) {
    handleClick() {
        sessionStorage.setItem("sample", JSON.stringify({ message: "サンプルテキスト", timestamp: new Date().toISOString() }));
        
        this[NavigationMixin.Navigate]({
            type: "comm__namedPage",
            attributes: {
                name: "Page2__c",
            },
        });
    }
}

保存した値はブラウザの開発者ツールから確認できます。
スクリーンショット 2025-01-20 19.08.01.png

セッションストレージからデータを取り出す

let data = sessionStorage.getItem("key");でデータを取得します。
sessionStorage.removeItem("key");でデータを削除します。

import { LightningElement } from 'lwc';

export default class SampleGetSessionStorage extends LightningElement {
    data;

    connectedCallback() {
        const item = sessionStorage.getItem("sample");
        if (item) {
            this.data = JSON.parse(item);
            sessionStorage.removeItem("sample");
        }
    }
}

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?