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 相关文章推荐
深入理解JavaScript系列(7) S.O.L.I.D五大原则之开闭原则OCP
Jan 15 Javascript
浅析JavaScript中的typeof运算符
Nov 30 Javascript
jquery中EasyUI使用技巧小结
Feb 10 Javascript
JS动态显示表格上下frame的方法
Mar 31 Javascript
Javascript将数字转化成为货币格式字符串
Jun 22 Javascript
JS留言功能的简单实现案例(推荐)
Jun 23 Javascript
jQuery将表单序列化成一个Object对象的实例
Nov 29 Javascript
jQuery插件开发发送短信倒计时功能代码
May 09 jQuery
Node.js 中使用 async 函数的方法
Nov 20 Javascript
JavaScript生成简单等差数列
Nov 28 Javascript
vue cli 3.x 项目部署到 github pages的方法
Apr 17 Javascript
js实现简单图片拖拽效果
Feb 22 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代码的53条建议
2008/03/27 PHP
PHP读取网页文件内容的实现代码(fopen,curl等)
2011/06/23 PHP
PHP时间和日期函数详解
2015/05/08 PHP
Yii快速入门经典教程
2015/12/28 PHP
IE和firefox浏览器的event事件兼容性汇总
2009/12/06 Javascript
javascript-简单的计算器实现步骤分解(附图)
2013/05/30 Javascript
extjs每个组件要设置唯一的ID否则会出错
2014/06/15 Javascript
jquery的ajax异步请求接收返回json数据实例
2014/06/16 Javascript
JS实现的网页倒计时数字时钟效果
2015/03/02 Javascript
js获取form的方法
2015/05/06 Javascript
javascript框架设计之浏览器的嗅探和特征侦测
2015/06/23 Javascript
详解vue静态资源打包中的坑与解决方案
2018/02/05 Javascript
vue动态绑定组件子父组件多表单验证功能的实现代码
2018/05/14 Javascript
js自定义input文件上传样式
2018/10/26 Javascript
微信小程序 wepy框架与iview-weapp的用法详解
2019/04/10 Javascript
详解关闭令人抓狂的ESlint 语法检测配置方法
2019/10/28 Javascript
小程序点餐界面添加购物车左右摆动动画
2020/09/23 Javascript
[01:03:13]VG vs Pain 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python创建和删除目录的方法
2015/04/29 Python
python之线程通过信号pyqtSignal刷新ui的方法
2019/01/11 Python
Python 限制线程的最大数量的方法(Semaphore)
2019/02/22 Python
Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法
2019/08/23 Python
python中绕过反爬虫的方法总结
2020/11/25 Python
HTML5 创建canvas元素示例代码
2014/06/04 HTML / CSS
friso美素佳儿官方海外旗舰店:荷兰原产原罐
2017/07/03 全球购物
Laura官网:加拿大女性的顶级时尚目的地
2019/09/20 全球购物
为什么使用接口?
2014/08/13 面试题
绿化工程实施方案
2014/03/17 职场文书
小学开学典礼主持词
2014/03/19 职场文书
养生餐厅创业计划书范文
2014/03/26 职场文书
借条如何写
2015/05/26 职场文书
python获取对象信息的实例详解
2021/07/07 Python
Java使用jmeter进行压力测试
2021/07/09 Java/Android
手把手教你导入Go语言第三方库
2021/08/04 Golang
python开发制作好看的时钟效果
2022/05/02 Python
在windows server 2012 r2中安装mysql的详细步骤
2022/07/23 Servers