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 } ];

相关内容

热门资讯

aapoker是正规的吗!AA... aapoker是正规的吗!AApoker辅助工具,(AApOKER)本来有挂,安装教程(黑科技透明)...
微扑克辅助测试!微扑克代打是真... 微扑克辅助测试!微扑克代打是真的吗,(微扑克)一直是有挂,2025新版总结(黑科技教程)1、点击下载...
德扑之星辅牌软件使用!德扑ai... 德扑之星辅牌软件使用!德扑ai智能工具,(德扑ai)果然是有挂,技巧教程(黑科技功能);1、构建自己...
德州ai辅助设备!德州之星辅助... 德州ai辅助设备!德州之星辅助软件,(德州)都是真的有挂,必赢方法(黑科技透明挂);1、该软件可以轻...
wpk发牌机制!wpk辅助外挂... wpk发牌机制!wpk辅助外挂,(WpK)原来有挂,2025新版教程(黑科技总结)1、任何wpk发牌...
德扑之星透明软件的使用方法!德... 德扑之星透明软件的使用方法!德扑之星软件靠普吗,(德扑ai)本来存在有挂,wpk教程(黑科技攻略)1...
wepoke有透视挂吗!wep... wepoke有透视挂吗!wepower作弊器,(wePokE)本来有挂,曝光教程(黑科技透明);1、...
wepoke app系统规律!... wepoke app系统规律!Wepoke透明挂,(we-poker)切实是有挂,介绍教程(黑科技黑...
aapoker透视辅助!aap... aapoker透视辅助!aapoker透视辅助器,(aa poker)竟然是真的有挂,分享教程(黑科...
aapoker有外挂吗!aap... aapoker有外挂吗!aapoker透明挂下载,(AAPOKer)其实存在有挂,靠谱教程(黑科技功...