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 编程引入命名空间的方法
Jun 29 Javascript
js document.write()使用介绍
Feb 21 Javascript
js截取中英文字符串、标点符号无乱码示例解读
Apr 17 Javascript
使用ajaxfileupload.js实现ajax上传文件php版
Jun 26 Javascript
jQuery实现统计复选框选中数量
Nov 24 Javascript
基于Javascript实现二级联动菜单效果
Mar 04 Javascript
JavaScript函数参数的传递方式详解
Mar 06 Javascript
Vue实例中生命周期created和mounted的区别详解
Aug 25 Javascript
小程序开发基础之view视图容器
Aug 21 Javascript
JS使用iView的Dropdown实现一个右键菜单
May 06 Javascript
微信小程序激励式视频广告组件使用详解
Dec 06 Javascript
详解JavaScript之ES5的继承
Jul 08 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
Yii2设置默认控制器的两种方法
2017/05/19 PHP
学习ExtJS border布局
2009/10/08 Javascript
js改变文章字体大小的实例代码
2013/11/27 Javascript
基于jquery的文字向上跑动类似跑马灯的效果
2014/09/22 Javascript
PHP配置文件php.ini中打开错误报告的设置方法
2015/01/09 PHP
JS限制文本框只能输入数字和字母方法
2015/02/28 Javascript
JS实现常见的TAB、弹出层效果(TAB标签,斑马线,遮罩层等)
2015/10/08 Javascript
基于javascript代码实现通过点击图片显示原图片
2015/11/29 Javascript
基于JavaScript实现全屏透明遮罩div层锁屏效果
2016/01/26 Javascript
canvas绘制一个常用的emoji表情
2017/03/30 Javascript
在vue项目中,使用axios跨域处理
2018/03/07 Javascript
JS用最简单的方法实现四舍五入
2019/08/27 Javascript
JS实现炫酷雪花飘落效果
2020/08/19 Javascript
浅谈vant组件Picker 选择器选单选问题
2020/11/04 Javascript
解决vue props传Array/Object类型值,子组件报错的情况
2020/11/07 Javascript
Python中使用urllib2防止302跳转的代码例子
2014/07/07 Python
Python网络爬虫出现乱码问题的解决方法
2017/01/05 Python
MAC中PyCharm设置python3解释器
2017/12/15 Python
Python字符串拼接六种方法介绍
2017/12/18 Python
在cmd命令行里进入和退出Python程序的方法
2018/05/12 Python
pycharm运行和调试不显示结果的解决方法
2018/11/30 Python
在Pycharm中对代码进行注释和缩进的方法详解
2019/01/20 Python
python ChainMap 合并字典的实现步骤
2019/06/11 Python
python全局变量引用与修改过程解析
2020/01/07 Python
Python sqlalchemy时间戳及密码管理实现代码详解
2020/08/01 Python
空指针到底是什么
2012/08/07 面试题
安全资料员岗位职责
2013/12/14 职场文书
学校安全教育制度
2014/01/31 职场文书
党的群众路线教育实践活动组织生活会发言材料
2014/10/17 职场文书
乡镇群众路线专项整治方案
2014/11/03 职场文书
幼儿园教师自我评价
2015/03/04 职场文书
有关三国演义的读书笔记
2015/06/25 职场文书
2016寒假社会实践心得体会范文
2015/10/09 职场文书
市级三好生竞选稿
2015/11/21 职场文书
【海涛教你打dota】体验一超神发条:咱是抢盾专业户
2022/04/01 DOTA
Python实战实现爬取天气数据并完成可视化分析详解
2022/06/16 Python