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

相关内容

热门资讯

透视真的!wepoker轻量版... 透视真的!wepoker轻量版透视系统,we-poker有人玩吗(透视)真是真的有挂;1、构建自己的...
透视新版!德普之星透视辅助插件... 透视新版!德普之星透视辅助插件(透视)辅助器app(原来真的有挂)该软件可以轻松地帮助玩家将德普之星...
透视有挂!wepoker有透视... 透视有挂!wepoker有透视吗,wejoker私人辅助软件(透视)原来存在有挂;1、该软件可以轻松...
透视神器!德普之星的辅助工具介... 透视神器!德普之星的辅助工具介绍(透视)透视挂(确实真的有挂)1、德普之星的辅助工具介绍ai辅助优化...
透视科技!wepoker插件下... 透视科技!wepoker插件下载,wepoker私人局俱乐部辅助(透视)本来存在有挂1、进入到wep...
透视数据!德普软件(透视)ap... 透视数据!德普软件(透视)app安卓版破解版(切实真的是有挂)1、首先打开德普软件最新版本,在德普软...
透视安卓版!wepoker私人... 透视安卓版!wepoker私人局外卦,we poker辅助器下载(透视)原来存在有挂;1、许多玩家不...
透视私人局!如何下载德普之星辅... 透视私人局!如何下载德普之星辅助软件(透视)透视免费(一贯真的是有挂)小薇(透视辅助)致您一封信;亲...
透视挂!wepoker透视有用... 透视挂!wepoker透视有用吗,wepoker透视功能下载(透视)都是真的是有挂1、实时wepok...
透视挂透视!德普之星怎么设置埋... 透视挂透视!德普之星怎么设置埋牌(透视)辅助器(确实是有挂)所有人都在同一条线上,像星星一样排成一排...