在Angular 6中,可以使用Fragment
来导航到同一页中的锚点。以下是一个示例:
首先,在你的组件模板中,使用锚点标记要导航的位置。例如:
Section 1
This is section 1.
Section 2
This is section 2.
Section 3
This is section 3.
然后,在组件的方法中,使用router.navigate
方法来导航到具体的锚点。例如:
import { Component } from '@angular/core';
import { Router, NavigationExtras } from '@angular/router';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent {
constructor(private router: Router) { }
navigateToSection(section: string) {
const navigationExtras: NavigationExtras = {
fragment: section
};
this.router.navigate([], navigationExtras);
}
}
最后,在模板中调用navigateToSection
方法,并传递要导航到的锚点。例如:
当你点击这些按钮时,页面会滚动到对应的锚点位置。
希望这个示例能帮助到你!