3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LWCでページ遷移時にクエリパラメータを渡す

Last updated at Posted at 2022-11-01

ページ遷移元

stateプロパティを設定することで、クエリパラメータを追加することができる。削除するときはundefinedを設定する。
stateプロパティは名前空間プレフィックス__で始める必要がある。管理パッケージでない場合はc__となる。
stateで使用できるのは文字列のみ。

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

export default class SampleA extends NavigationMixin(LightningElement) {
    navigateToRecordViewPage() {
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                objectApiName: 'SObjectName__c',
                recordId: this.selectedId
            },
            state: {
                c__viewType: 'simple' // クエリパラメータを設定
            }
        });
    }
}

ページ遷移先

CurrentPageReferenceを利用して、currentpagereference.state.c__xxxからクエリパラメータを取得する。

import { LightningElement, wire } from "lwc";
import { CurrentPageReference } from "lightning/navigation";

export default class SampleB extends LightningElement {
    currentPageReference;

    @wire(CurrentPageReference)
    setCurrentPageReference(currentPageReference) {
        this.currentPageReference = currentPageReference;
    }

    get viewType() {
        return this.currentPageReference ? this.currentPageReference.state.c__viewType : undefined;
    }
}

参考

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?