Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

routing is not working in src/index.html angular

Routing is not working in src/index.html angular.

I mean I have a header some links there to navigate:

    <header>
      <div class="logo">
        <div class="logo-img-div">
          <img src="../../assets/img/logo.png" alt="" class="logo-img" />
        </div>
      </div>

      <nav>
        <div class="elements">
          <ul>
            <li class="header-item">
              <a routerLink="/">Home</a>
            </li>
            <li class="header-item">
              <a routerLink="/category">Category</a>
            </li>
            <li class="header-item">
              <a routerLink="/about">About us</a>
            </li>
            <li class="header-item">
              <a routerLink="/contact">Contact us</a>
            </li>
          </ul>
        </div>
      </nav>
    </header>

But when I click on, nothing happens. I have also added paths properly, as in the component if I paste the same code, it works fine but appears twice.

like image 923
Yogesh Aggarwal Avatar asked Jan 25 '23 20:01

Yogesh Aggarwal


2 Answers

routerLink is an angular directive, it won't work in index.html, It works inside a component of some module. You can copy your code to app.component.html

For more details, read this article

like image 127
Adrita Sharma Avatar answered Jan 28 '23 09:01

Adrita Sharma


That is because your index.html is placeholder for angular to render the component.

You are using routerLink inside index.html but you dont have router module so you cant use routerLink.

That kind of routing only work inside angular component

like image 22
Tony Ngo Avatar answered Jan 28 '23 10:01

Tony Ngo