Angular碎片导航无法工作。
创始人
2024-10-29 16:30:47
0次
  1. 在app.module.ts文件中导入RouterModule,并在imports数组中添加RouterModule.forRoot([])。

import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [ // 定义路由 ];

@NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }

  1. 在HTML模板中,使用RouterLink指令将链接绑定到片段标识符。

Link to Section 1

  1. 在组件中,可以使用ActivatedRoute服务来获取当前路由的片段标识符。

import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router';

@Component({ selector: 'app-section', templateUrl: './section.component.html', styleUrls: ['./section.component.css'] }) export class SectionComponent implements OnInit { fragment: string;

constructor(private route: ActivatedRoute) { }

ngOnInit() { this.route.fragment.subscribe(fragment => { this.fragment = fragment; }); } }

  1. 如果要在初始化时滚动到片段,则可以使用ViewChild和ElementRef获取DOM元素,并使用scrollIntoView方法滚动到该元素。

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; import { ActivatedRoute } from '@angular/router';

@Component({ selector: 'app-section', templateUrl: './section.component.html', styleUrls: ['./section.component.css'] }) export class SectionComponent implements OnInit { @ViewChild('section1') section1: ElementRef;

constructor(private route: ActivatedRoute) { }

ngOnInit() { this.route.fragment.subscribe(fragment => { if (fragment === 'section1') { this.section1.nativeElement.scrollIntoView(); } }); } }

注意:片段标识符需要在路由定义中定义为一个参数。例如:

const routes: Routes = [ { path: 'page', component: PageComponent }, { path: 'page/:fragment', component: PageComponent } ];

相关内容

热门资讯

专业讨论开挂app!天听之旅多... 专业讨论开挂app!天听之旅多功能开挂app,天听之旅竟然真的是有开挂app1、每一步都需要思考,不...
一起来讨论作弊器方法!哈哈长沙... 一起来讨论作弊器方法!哈哈长沙麻将多功能作弊器方法,哈哈长沙麻将本来是真的有作弊器方法哈哈长沙麻将辅...
一分钟揭秘作弊器软件!星火燎原... 一分钟揭秘作弊器软件!星火燎原多功能作弊器软件,星火燎原原来存在有作弊器软件1、星火燎原辅助器安装包...
最新技巧透视器插件!聚贤多功能... 最新技巧透视器插件!聚贤多功能透视器插件,聚贤本来有透视器插件运聚贤辅助工具,进入游戏界面。进入聚贤...
必备科技作弊器app!白金岛红... 必备科技作弊器app!白金岛红拐弯多功能作弊器app,白金岛红拐弯果然存在有作弊器app1、进入到白...
总算了解辅助插件!欢乐划水麻将... 总算了解辅助插件!欢乐划水麻将多功能辅助插件,欢乐划水麻将好像是真的有辅助插件1、用户打开应用后不用...
如何分辨真伪外挂工具!朋朋政和... 如何分辨真伪外挂工具!朋朋政和麻将多功能外挂工具,朋朋政和麻将其实有外挂工具1、这是跨平台的朋朋政和...
普及知识辅助脚本!大同麻将多功... 普及知识辅助脚本!大同麻将多功能辅助脚本,大同麻将原来真的有辅助脚本1、完成大同麻将有辅助插件,帮助...
发现玩家外挂app!乐通多功能... 发现玩家外挂app!乐通多功能外挂app,乐通竟然真的是有外挂app1、玩家可以在乐通透视最简单三个...
指导大家透视器工具!朋朋政和麻... 指导大家透视器工具!朋朋政和麻将多功能透视器工具,朋朋政和麻将其实存在有透视器工具1、金币登录送、破...