Angular 利用路由跳转到指定页面的指定位置方法
网络编程 2021-07-04 16:46www.168986.cn编程入门
今天长沙网络推广就为大家分享一篇Angular 利用路由跳转到指定页面的指定位置方法,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
之前做过页面内的跳转,比较简单,最近项目需要实现跨页面跳转,并跳转到指定地点,试了很多方法,有用到传递参数然后让页面滚动相应的距离,但是一旦文章长短发生变化,滚动的距离也需要重新计算,比较麻烦,所以最后总结出这两种比较靠谱的方法,只需要在需要跳转的地方加上合适的id就行,原理和单页面内跳转相似。
detail.ponent.html
<p>You'll see which payment methods are available to you on the checkout page, before you submit a reservation request. After you select your country, all of your payment details will be shown.</p> <p id="readMore">We charge featured guide who offer journey a 15% service fee. The amount of the service fee is calculated from the price that featured guide set for their journey. You will see the service fee when you set your price before submitting a journey. The service fee is automatically deducted from the payout to the Host. Depending on the laws of the jurisdiction involved, VAT may be charged on of the service fee. The service fee will include these VAT charges when applicable.</p>
app.ponent.html
<button (click)="readMore()">ReadMore</button>
app.route.ts
{ path: '',ponent: LoginComponent}, { path: 'detail', ponent: DetailComponent }, { path: '**',ponent: NotFoundComponent}
方法一:新增路由地址来实现
app.route.ts
{ path: '',ponent: LoginComponent}, { path: 'detail', ponent: DetailComponent }, { path: 'detail#readMore',ponent: NotFoundComponent}, { path: '**',ponent: NotFoundComponent}
app.ponent.ts
readMore() { this.router.navigateByUrl('/detail#readMore'); }
detail.ponent.ts
ngOnInit() { if (window.location.hash === '#readMore') { window.location.assign('detail#readMore'); } }
方法二:在原路由地址不变的情况下,利用路由传递参数来实现
app.ponent.ts
readMore() { this.router.navigate(['/detail', { id: 'readMore'}]); } detail.ponent.ts this.myActivatedRoute.params.subscribe( (data: any) => { if (data.id === 'readMore') { window.location.assign('detail#readMore'); } } );
以上这篇Angular 利用路由跳转到指定页面的指定位置方法就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程