Angular:无法路由回Web应用程序的某个特定部分
创始人
2024-11-02 02:00:42
0
  1. 使用Fragment进行导航

在导航时,将URL的Fragment设置为你想要导航回的特定部分。例如:如果你想导航回ID为“section2”的部分,你可以使用以下代码:

router.navigate([], { fragment: 'section2' });

然后在你的组件中使用ViewChild指令来访问该部分。例如:

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

@Component({ selector: 'app-my-component', template:

Section 1
Section 2
}) export class MyComponent implements OnInit { @ViewChild('section1') section1: ElementRef; @ViewChild('section2') section2: ElementRef;

constructor(private router: Router, private route: ActivatedRoute) { }

ngOnInit() { this.route.fragment.subscribe(fragment => { if (fragment === 'section2') { this.scrollTo(this.section2); } }); }

private scrollTo(element: ElementRef) { element.nativeElement.scrollIntoView(); } }

  1. 使用queryParams进行导航

如果你使用的是queryParams而不是Fragment进行导航,则可以将你想要导航回的特定部分转换为参数,并在导航时传递它。例如:

router.navigate([], { queryParams: { section: 'section2' } });

然后在你的组件中使用ActivatedRoute来获取该参数,并使用ViewChild指令访问该部分。例如:

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

@Component({ selector: 'app-my-component', template:

Section 1
Section 2
}) export class MyComponent implements OnInit { @ViewChild('section1') section1: ElementRef; @ViewChild('section2') section2: ElementRef;

constructor(private

相关内容

热门资讯

据目击者称"欢乐对决... 据目击者称"欢乐对决脚本辅助器ios"好像真的是有辅助工具(哔哩哔哩)暗藏猫腻,小编详细说明欢乐对决...
透视黑科技"微信决胜... 透视黑科技"微信决胜游戏辅助"原来真的是有辅助攻略(哔哩哔哩)1、微信决胜游戏辅助有没有辅助教程、微...
技巧辅助挂"欢乐对决... 技巧辅助挂"欢乐对决手游辅助"总是存在有辅助方法(哔哩哔哩)1、超多福利:超高返利,海量正版游戏,欢...
据了解"新众乐辅助&... 据了解"新众乐辅助"都是是有辅助脚本(哔哩哔哩)1、新众乐辅助有没有辅助教程、新众乐辅助有透视吗等服...
透视挂透视"福建天天... 透视挂透视"福建天天开心辅助器"好像是有辅助方法(哔哩哔哩)1)福建天天开心辅助器免费钻石:进一步探...
透视玄学"边锋老友怎... 透视玄学"边锋老友怎么开挂"都是是真的有辅助app(哔哩哔哩)1、边锋老友怎么开挂模拟器是什么优化,...
黑科技攻略"大唐山西... 黑科技攻略"大唐山西辅助软件"都是确实有辅助神器(哔哩哔哩)1、很好的工具软件,可以解锁游戏的大唐山...
受玩家影响"微信中至... 受玩家影响"微信中至有辅助吗"果然是真的有辅助软件(哔哩哔哩)1、在微信中至有辅助吗插件功能辅助器技...
事发当天"闲来辅助神... 事发当天"闲来辅助神器下载2022"竟然是有辅助插件(哔哩哔哩)1、闲来辅助神器下载2022免费脚本...
透视透视"天天爱柳州... 透视透视"天天爱柳州辅助器"真是确实有辅助工具(哔哩哔哩)1、让任何用户在无需天天爱柳州辅助器安装教...