Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The data-navigo attribute does not work in the Lit component. #337

Open
wencan opened this issue Nov 10, 2024 · 0 comments
Open

The data-navigo attribute does not work in the Lit component. #337

wencan opened this issue Nov 10, 2024 · 0 comments

Comments

@wencan
Copy link

wencan commented Nov 10, 2024

Clicking on the Home and About links triggers internal navigation within the SPA.
However, clicking on the Contact link causes the browser to reload the page.

<!DOCTYPE html>
<html>
<head>
    <title>Lit + Navigo SPA</title>
    <script type="module">
        import { LitElement, html, css } from 'https://gcore.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js';
        import Navigo from 'https://cdn.skypack.dev/navigo';

        class AppRoot extends LitElement {
            static styles = css`
                :host {
                    display: block;
                }
                nav {
                    background: #f8f9fa;
                    padding: 1rem;
                }
                nav a {
                    margin-right: 1rem;
                    text-decoration: none;
                    color: #007bff;
                }
                nav a:hover {
                    text-decoration: underline;
                }
                main {
                    padding: 1rem;
                }
            `;

            constructor() {
                super();
                this.currentPage = html`<home-page></home-page>`;
                this.router = new Navigo('/');
            }
            
            
            connectedCallback() {
                super.connectedCallback();
                
                this.router
                    .on('/', () => {
                        this.currentPage = html`<div>Home</div>`;
                        this.requestUpdate();
                    })
                    .on('/about', () => {
                        this.currentPage = html`<div>About</div>`;
                        this.requestUpdate();
                    })
                    .on('/contact', () => {
                        this.currentPage = html`<div>Contact</div>`;
                        this.requestUpdate();
                    })
                    .resolve();
            }

            render() {
                return html`
                    <nav>
                        <a href="/" @click=${(event) => { event.preventDefault(); this.router.navigate('/'); }}>Home</a>
                        <a href="/about" @click=${(event) => { event.preventDefault(); this.router.navigate('/about'); }}>About</a>
                        <a href="/contact" data-navigo>Contact</a>

                    </nav>
                    <main>
                        ${this.currentPage}
                    </main>
                `;
            }
        }
        customElements.define('app-root', AppRoot);
        
        window.addEventListener('load', () => {
            console.log('Page was reloaded');
        });
    </script>
</head>
<body>
    <app-root></app-root>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant