Angular 利用路由跳转到指定页面的指定位置方法


Posted in Javascript onAugust 31, 2018

之前做过页面内的跳转,比较简单,最近项目需要实现跨页面跳转,并跳转到指定地点,试了很多方法,有用到传递参数然后让页面滚动相应的距离,但是一旦文章长短发生变化,滚动的距离也需要重新计算,比较麻烦,所以最后总结出这两种比较靠谱的方法,只需要在需要跳转的地方加上合适的id就行,原理和单页面内跳转相似。

detail.component.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 top of the service fee. The service fee will include these VAT charges when applicable.</p>

app.component.html

<button (click)="readMore()">ReadMore</button>

app.route.ts

{ path: '',component: LoginComponent},
 { path: 'detail', component: DetailComponent },
 { path: '**',component: NotFoundComponent}

方法一:新增路由地址来实现

app.route.ts

{ path: '',component: LoginComponent},
 { path: 'detail', component: DetailComponent },
 { path: 'detail#readMore',component: NotFoundComponent},
 { path: '**',component: NotFoundComponent}

app.component.ts

readMore() {
this.router.navigateByUrl('/detail#readMore');
 }

detail.component.ts

ngOnInit() {
 if (window.location.hash === '#readMore') {
  window.location.assign('detail#readMore');
 }
 }

方法二:在原路由地址不变的情况下,利用路由传递参数来实现

app.component.ts

readMore() {
 this.router.navigate(['/detail', { id: 'readMore'}]);
 }
detail.component.ts

this.myActivatedRoute.params.subscribe(
  (data: any) => {
  if (data.id === 'readMore') {
   window.location.assign('detail#readMore');
  }
  }
 );

以上这篇Angular 利用路由跳转到指定页面的指定位置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js获取指定日期前后的日期代码
Aug 20 Javascript
js Math 对象的方法
Sep 01 Javascript
jquery序列化表单以及回调函数的使用示例
Jul 02 Javascript
一个JavaScript处理textarea中的字符成每一行实例
Sep 22 Javascript
javascript解析json实例详解
Nov 05 Javascript
浅谈javascript对象模型和function对象
Dec 26 Javascript
js带缩略图的图片轮播效果代码分享
Sep 14 Javascript
JavaScript中的定时器之Item23的合理使用
Oct 30 Javascript
JavaScript中的this使用详解
Jul 27 Javascript
AngularJS  $modal弹出框实例代码
Aug 24 Javascript
微信小程序结合mock.js实现后台模拟及调试
Mar 28 Javascript
javascript对象3个属性特征
Nov 17 Javascript
Vue 莹石摄像头直播视频实例代码
Aug 31 #Javascript
JavaScript实现简单的隐藏式侧边栏功能示例
Aug 31 #Javascript
Vue加载组件、动态加载组件的几种方式
Aug 31 #Javascript
Bootstrap模态对话框用法简单示例
Aug 31 #Javascript
微信小程序视图容器(swiper)组件创建轮播图
Jun 19 #Javascript
angular 实时监听input框value值的变化触发函数方法
Aug 31 #Javascript
vuejs实现ready函数加载完之后执行某个函数的方法
Aug 31 #Javascript
You might like
十天学会php之第三天
2006/10/09 PHP
PHP 采集心得技巧
2009/05/15 PHP
Laravel 5 学习笔记
2015/03/06 PHP
浅谈PHP进程管理
2019/03/08 PHP
js滚动条多种样式,推荐
2007/02/05 Javascript
网页前端优化之滚动延时加载图片示例
2013/07/13 Javascript
javascript实现数字验证码的简单实例
2014/02/10 Javascript
jQuery提示插件alertify使用指南
2015/04/21 Javascript
jQuery实现checkbox全选的方法
2015/06/10 Javascript
微信小程序 wxapp内容组件 progress详细介绍
2016/10/31 Javascript
关于Node.js的events.EventEmitter用法介绍
2017/04/01 Javascript
Bootstrap popover 实现鼠标移入移除显示隐藏功能方法
2018/01/24 Javascript
JS+canvas画布实现炫酷的旋转星空效果示例
2019/02/13 Javascript
微信小程序自定义头部导航栏和导航栏背景图片 navigationStyle问题
2019/07/26 Javascript
JavaScript实现横版菜单栏
2020/03/17 Javascript
微信小程序实现登录注册功能
2020/12/29 Javascript
[02:27]DOTA2英雄基础教程 莱恩
2014/01/17 DOTA
[41:54]2018DOTA2亚洲邀请赛 4.1 小组赛A组加赛 TNC vs Liquid
2018/04/03 DOTA
用Python的线程来解决生产者消费问题的示例
2015/04/02 Python
python机器学习理论与实战(一)K近邻法
2021/01/28 Python
解决python通过cx_Oracle模块连接Oracle乱码的问题
2018/10/18 Python
python找出完数的方法
2018/11/12 Python
使用matplotlib中scatter方法画散点图
2019/03/19 Python
Python中使用pypdf2合并、分割、加密pdf文件的代码详解
2019/05/21 Python
Python操作远程服务器 paramiko模块详细介绍
2019/08/07 Python
在jupyter notebook 添加 conda 环境的操作详解
2020/04/10 Python
俄罗斯鲜花递送:AMF
2020/04/24 全球购物
Windows和Linux动态库应用异同
2016/07/28 面试题
工商管理实习自我鉴定
2013/09/28 职场文书
中专生自我鉴定
2013/12/17 职场文书
新员工欢迎词
2014/01/12 职场文书
应聘会计求职信
2014/06/11 职场文书
2014年控辍保学工作总结
2014/12/08 职场文书
仓库统计员岗位职责
2015/04/14 职场文书
MySQL 表锁定 LOCK和UNLOCK TABLES的 SQL语法
2022/04/18 MySQL
Nginx配置使用详解
2022/07/07 Servers