简体   繁体   English

如何从一个组件路由到 Angular 中另一个组件的一部分?

[英]How can I route from a component to a section of another component in Angular?

I have two components and they are situtated in the different routes.我有两个组件,它们位于不同的路线中。 In the Second component There is a div.在第二个组件中有一个 div。 I want to route from first component to the div of the second component.我想从第一个组件路由到第二个组件的 div。 Yes, It is simple to route the second component.是的,布线第二个组件很简单。 But I want to scroll be top of the div.但我想滚动到 div 的顶部。 在此处输入图像描述

Thank you for answers.谢谢你的回答。

This tag is declared in component1该标记在 component1 中声明

<a [routerLink] = "['/c2']" fragment="c2id"> Link </a>

Here are the component2 changes这是 component2 的变化

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
  selector: 'app-c2',
  templateUrl: './c2.component.html',
  styleUrls: ['./c2.component.css']
})
export class C2Component implements OnInit {
  private fragment: string;

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.route.fragment.subscribe(fragment => {
      this.fragment = fragment;
    });
  }
  ngAfterViewInit(): void {
    try {
      document.querySelector('#' + this.fragment).scrollIntoView();
    } catch (e) {}
  }
}

And your component2 html will be like this而你的component2 html会是这样的

<p style="height: 800px;">
c2 works!
</p>
<hr>
<div id="c2id" style="height: 500px;">
  The div with c2id
</div>

Here is the updated and working stackblitz https://angular-fragment-example.stackblitz.io这是更新和工作的 stackblitz https://angular-fragment-example.stackblitz.io

I think you are looking for Fragments .我认为您正在寻找Fragments

Official Docs: Angular Docs- Query Params and Fragments官方文档: Angular Docs-Query Params and Fragments

Examples:例子:

Manual Navigation手动导航

in c1.htmlc1.html

<a [routerLink] = "['/c2']" [fragment]="c2id"> Link </a>

in c2.htmlc2.html

<div id="c2id">content</div>

Programatic Navigation程序化导航

in c1.tsc1.ts

private fragmentSetDynamically: string;
constructor(private router: Router){}

onClickButton(){
   this.router.navigate(['/c2'], {fragment: fragmentSetDynamically});
}

Getting the fragment:获取片段:

in c2.tsc2.ts

private fragment: string;
constructor(private activatedRoute: ActivatedRoute){}

ngOnInit(){
  this.fragment = this.activatedRoute.snapshot.fragment;
}

I have two components and they are situtated in the different routes.我有两个组件,它们位于不同的路线上。 In the Second component There is a div.在第二个组件中有一个 div。 I want to route from first component to the div of the second component.我想从第一个组件路由到第二个组件的 div。 Yes, It is simple to route the second component.是的,路由第二个组件很简单。 But I want to scroll be top of the div.但我想滚动到 div 的顶部。 在此处输入图像描述

Thank you for answers.谢谢你的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Angular 6 中的另一个组件的打字稿激活和停用组件中的样式? - How can I activate and deactivate styles in component from typescript from another component in Angular 6? 如何从 Angular 中的另一个组件滚动到一个组件? - How to scroll to a component from another component in Angular? 我如何使用 NavLink 从组件重定向到另一个组件 - how can i use NavLink to redirect from component to another component 如何从Angular 2的内部组件中进行布线? - How to route from within an inner component in Angular 2? 我如何将点击事件定位到 Angular 2 中的另一个组件 - How can i target a click event to another component in Angular 2 如何以角度从另一个组件销毁活动路由组件 - How to Destroy Active Routing Component from another component in angular 如何从调用另一个组件 angular 的组件发送数据 - How to send data from a component which is calling another component angular Angular:引导程序 - 如何在从其他组件调用时在组件中使用全高? - Angular : Bootstrap - How can I use full height In a component while calling from other component? 如何使用 Vue draggable 将项目从组件中的列表拖动到另一个组件中的列表? - How can I drag items from list in component to list inside another component using Vue draggable? 如何在Angular中加载整个组件? - How can I load whole component in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM