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脚本实现Web页面信息交互
Dec 21 Javascript
js Array操作的最简短最容易理解方法
Dec 09 Javascript
按Enter键触发事件的jquery方法实现代码
Feb 17 Javascript
javascript引用类型指针的工作方式
Apr 13 Javascript
跟我学习javascript的for循环和for...in循环
Nov 18 Javascript
js基础之DOM中元素对象的属性方法详解
Oct 28 Javascript
JavaScript中this的用法及this在不同应用场景的作用解析
Apr 13 Javascript
javascript帧动画(实例讲解)
Sep 02 Javascript
three.js实现3D影院的原理的代码分析
Dec 18 Javascript
jquery实现二级导航下拉菜单效果实例
May 14 jQuery
vue动态绑定class的几种常用方式小结
May 21 Javascript
基于JavaScript实现十五拼图代码实例
Apr 26 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 chmod 函数与批量修改文件目录权限
2010/05/10 PHP
php与paypal整合方法
2010/11/28 PHP
ThinkPHP连接Oracle数据库
2016/04/22 PHP
JSON两种结构之对象和数组的理解
2016/07/19 PHP
Zend Framework上传文件重命名的实现方法
2016/11/25 PHP
JavaScript的变量作用域深入理解
2009/10/25 Javascript
js判断一个元素是否为另一个元素的子元素的代码
2012/03/21 Javascript
使用Java实现简单的server/client回显功能的方法介绍
2013/05/03 Javascript
jquery图片放大功能简单实现
2013/08/01 Javascript
JavaScript 实现打印,打印预览,打印设置
2014/12/30 Javascript
js实现数组转换成json
2015/06/26 Javascript
javascript实现确定和取消提示框效果
2015/07/10 Javascript
jQuery焦点图切换特效代码分享
2015/09/15 Javascript
js和jquery实现监听键盘事件示例代码
2020/06/24 Javascript
基于MVC5和Bootstrap的jQuery TreeView树形控件(一)之数据支持json字符串、list集合
2016/08/11 Javascript
JS+CSS3模拟溢出滚动效果
2016/08/12 Javascript
分分钟玩转Vue.js组件
2016/10/25 Javascript
半个小时学json(json传递示例)
2016/12/25 Javascript
JS获取日期的方法实例【昨天,今天,明天,前n天,后n天的日期】
2017/09/28 Javascript
详解Vue SPA项目优化小记
2018/07/03 Javascript
Vue中用JSON实现刷新界面不影响倒计时
2020/10/26 Javascript
[05:09]2016国际邀请赛中国区预选赛淘汰赛首日精彩回顾
2016/06/29 DOTA
解决python3 urllib中urlopen报错的问题
2017/03/25 Python
python3模块smtplib实现发送邮件功能
2018/05/22 Python
python得到单词模式的示例
2018/10/15 Python
Anaconda和ipython环境适配的实现
2020/04/22 Python
python中查看.db文件中表格的名字及表格中的字段操作
2020/07/07 Python
jupyter使用自动补全和切换默认浏览器的方法
2020/11/18 Python
详解pandas赋值失败问题解决
2020/11/29 Python
利用纯css3实现的文字亮光特效的代码演示
2014/11/27 HTML / CSS
上海天奕面试题笔试题
2015/04/19 面试题
军训 自我鉴定
2014/02/03 职场文书
党支部创先争优活动总结
2014/08/28 职场文书
2014年社区妇联工作总结
2014/12/02 职场文书
实习介绍信范文
2015/05/05 职场文书
开学典礼校长致辞
2015/07/29 职场文书