[SalesForce] LWC get url parameters in Community

I'm trying to retrieve url parameters through a lightning web component in my Community.

For exemple :
wwww.my-community.com/my-page?test=true

How can I, in the new LWC framework, get the parameter "test" and it's value ?

Thanks for your answers 😉

Best Answer

If the question was if the LWC Framework provide an utility - then no not that i am aware of.

Which means you can use every js snippet you will find somewhere in the web like

    parameters = {};

    connectedCallback() {

        this.parameters = this.getQueryParameters();
        console.log(this.parameters);
    }

    getQueryParameters() {

        var params = {};
        var search = location.search.substring(1);

        if (search) {
            params = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}', (key, value) => {
                return key === "" ? value : decodeURIComponent(value)
            });
        }

        return params;
    }

That will return you an key/value pair of your parameters for further usage in your component