5

I am trying to converting normal bootstrap template into angular website. I am facing issue in routing while navigating from one page to another like About to Contact vice versa.

The template already has below format which using some css for smooth scrolling with help of hashtag #.

my app.compo.html

Before : <li><a href="#header">About</a></li>

after : <li><a routerLink="/about" routerLinkActive="active" href="#header">About</a></li>

<a href="#header" id="btp" class="back-to-top btn-floating waves-effect waves-light btn-large custom-btn">
    <i class="ion-ios-arrow-up"></i>
</a>

app.routing.ts

const routes: Routes = [
{ path: '', redirectTo: 'about', pathMatch: 'full' },
  { path: 'about', component: about, data: { state: 'about'} },
  { path: 'contact', component: contact, data: { state: 'contact'} },
];
export const AppRouting = RouterModule.forRoot(routes, { 
  useHash: true
});

After I hit npm start, its showing like below in my console

** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2018-04-05T05:55:06.359Z
Hash: 0bace8e39ad063fd5145
Time: 3614ms
chunk {inline} inline.bundle.js (inline) 3.85 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 2.91 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 577 bytes [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 46 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 852 kB [initial] [rendered]

ERROR in Cannot read property 'length' of undefined

webpack: Failed to compile.

This is my first attempt with angular 2. I am not able to understand official docs and other related threads for my issue.

kindly direct me to right way. if possible please share some plunker or stackblitz example

Mr. Learner
  • 978
  • 2
  • 18
  • 48
  • @Akanksha is right, just set `useHash` flag to `false` and no need to change other `href's`... angular's routing is based on routerLink and router oultets. see this example created for you on slackblithz.. https://angular-hxzu3s.stackblitz.io/ – miiiii Apr 05 '18 at 09:17
  • @Manoj Shukla tried.. but issue still persisting.. can you pls show stackblitz code? not able see your code formation – Mr. Learner Apr 05 '18 at 09:32
  • 1
    Sure.. Here is the complete link... https://stackblitz.com/edit/angular-hxzu3s?embed=1&file=app/app.component.html&view=editor – miiiii Apr 05 '18 at 09:33
  • plz lemme know if it's still not working.. !! Will try few more options. :) – miiiii Apr 05 '18 at 09:34
  • i did some changes as you said. but its still not working – Mr. Learner Apr 05 '18 at 09:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168282/discussion-between-manoj-shukla-and-worstcoder). – miiiii Apr 05 '18 at 09:39