简体   繁体   English

路由问题 - 哈希标记 - Angular 2

[英]Issue in Routing - Hash Tag - Angular 2

I am trying to converting normal bootstrap template into angular website. 我试图将正常的bootstrap模板转换为angular网站。 I am facing issue in routing while navigating from one page to another like About to Contact vice versa. 我在路由中遇到问题,同时从一个页面导航到另一个页面,如About Contact反之亦然。

The template already has below format which using some css for smooth scrolling with help of hashtag # . 模板已经具有以下格式,使用一些css在hashtag #帮助下进行平滑滚动。

my app.compo.html 我的app.compo.html

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

after : <li><a routerLink="/about" routerLinkActive="active" href="#header">About</a></li> 之后: <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 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 在我点击npm start之后 ,它在我的控制台中显示如下

** 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. 这是我第一次尝试角度2.我无法理解我的问题的官方文档和其他相关主题。

kindly direct me to right way. 请指引我正确的方式。 if possible please share some plunker or stackblitz example 如果可能请分享一些plunker或stackblitz示例

This could be done without using href hash (#) functionality.. 这可以在不使用href hash (#)功能的情况下完成。

Here is a working example.. 这是一个有效的例子..

https://stackblitz.com/edit/angular-hxzu3s?embed=1&file=app/app.module.ts&view=preview https://stackblitz.com/edit/angular-hxzu3s?embed=1&file=app/app.module.ts&view=preview


One more way to do is, using hash # 还有一种方法是使用hash #

Here is another working example.. 这是另一个工作实例..

https://stackblitz.com/edit/angular-ji6grr?embed=1&file=app/app.component.html&view=preview https://stackblitz.com/edit/angular-ji6grr?embed=1&file=app/app.component.html&view=preview

Can you try by removing href="#header" as it needs routerLink property only. 您可以尝试删除href =“#header”,因为它只需要routerLink属性。 And need to add router-outLet where you want to load that component. 并且需要在您要加载该组件的位置添加router-outLet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM