要解决Angular 8中Ng2PageScrollModule无法正常滚动的问题,可以尝试以下解决方法:
确保安装了Ng2PageScrollModule依赖: 在项目根目录下运行以下命令:
npm install --save ng2-page-scroll
在app.module.ts文件中导入Ng2PageScrollModule:
import { Ng2PageScrollModule } from 'ng2-page-scroll';
@NgModule({
imports: [
Ng2PageScrollModule.forRoot()
],
...
})
export class AppModule { }
在需要使用滚动功能的组件中导入PageScrollService:
import { PageScrollService } from 'ng2-page-scroll';
@Component({
...
})
export class MyComponent implements OnInit {
constructor(private pageScrollService: PageScrollService) { }
ngOnInit() {
}
scrollToElement(elementId: string): void {
this.pageScrollService.scroll({
document: document,
scrollTarget: elementId,
});
}
}
在模板文件中调用scrollToElement函数:
确保目标元素的id与scrollToElement函数中的scrollTarget参数值一致。
通过以上步骤,应该可以解决Angular 8中Ng2PageScrollModule无法正常滚动的问题。