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

相关内容

热门资讯

wepoke一定有挂!德扑之星... wepoke一定有挂!德扑之星带入记分牌,德州之星手游辅助,辅助教程(有挂教学)是一款可以让一直输的...
WePoKe透视挂!hm3德州... WePoKe透视挂!hm3德州辅助,wepoke用模拟器,细节揭秘(有挂了解)是一款可以让一直输的玩...
WePoKe透视挂!轰趴大菠萝... WePoKe透视挂!轰趴大菠萝十三水有外挂,aapoker软件讲解,曝光教程(有挂介绍),您好,We...
德扑之星有作弊!微扑克有挂,微... 德扑之星有作弊!微扑克有挂,微扑克有稳赢的打法,黑科技教程(有挂方法)1.德扑之星有作弊 ai辅助创...
微扑克wpk透视辅助!wpk微... 微扑克wpk透视辅助!wpk微扑克俱乐部,德州poker有外挂,技巧教程(有挂介绍)1、微扑克wpk...
wepower有外挂!微扑克w... wepower有外挂!微扑克wpk有挂吗,微扑克软件开发,力荐教程(有挂透明)1)wepower有外...
德扑之星有作弊!wepoke怎... 德扑之星有作弊!wepoke怎么来好牌,wpk微扑克真的有辅助,力荐教程(有挂技巧);小薇(透视辅助...
aapoker有猫腻!云扑克有... 您好,aapoker有猫腻这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
德州ai机器人!pokerma... 德州ai机器人!pokermaster有外挂吗,wopoker手机辅助,2025新版总结(有挂规律)...
aapoker有挂!wepok... aapoker有挂!wepokeai代打逻辑,微扑克代打,攻略教程(有挂规律),您好,wepokea...