[英]I cant navigate to the index page, when i click back button in Angular
I have Angular project.我有 Angular 项目。 on the index page I have buttons, which drives me in another page.
在索引页上我有一些按钮,这让我进入了另一个页面。 when I want to go back on index page, I click back button of browser and then comes just a white page, not my index page.
当我想 go 返回索引页时,我单击浏览器的后退按钮,然后出现一个白页,而不是我的索引页。 did u have same issue?
你有同样的问题吗?
As far as I know this is not an issue with angular routing.据我所知,这不是 angular 路由的问题。 I would suggest checking that the routing is configured correctly.
我建议检查路由配置是否正确。
Assuming that you have set up angular routing in your project ( this ), you have to ensure that the route for your index page is registered correctly.假设您已经在您的项目中设置了 angular 路由 ( this ),您必须确保正确注册索引页面的路由。 In your
app-routing.module.ts
there should be something like this:在你的
app-routing.module.ts
中应该有这样的东西:
const routes: Routes = [
{ path: 'another-page', component: AnotherPageComponent },
{ path: '', component: IndexPageComponent },
{ path: '**', redirectTo: '' }
]
In your app.module.ts
, ensure that the BrowserModule
and the AppRoutingModule
have been imported.在您的
app.module.ts
中,确保已导入BrowserModule
和AppRoutingModule
。
If angular routing is configured correctly, you could maybe enable route debugging to find the problem.如果 angular 路由配置正确,你可以启用路由调试来查找问题。 Other answers suggest that this might be helpful ( here , here ).
其他答案表明这可能会有所帮助(此处,此处)。
I fixed this issue with following: I gave my index page (app.component.html) class "mainPage"我通过以下方式解决了这个问题:我给了我的索引页(app.component.html)class“mainPage”
<div class="mainPage" #mainPage>
... // index page
</div>
Then I wrote following code in app.component.ts:然后我在 app.component.ts 中写了如下代码:
export class AppComponent implements OnInit{
@ViewChild('mainPage') buttons!: ElementRef;
constructor(private renderer: Renderer2, private el: ElementRef, private router: Router){
router.events.subscribe((eve : any )=>{
this.currentRoute = router.url;
if(this.currentRoute == '/'){
this.showIndex();
}
});
}
currentRoute : string = "";\
ngOnInit(): void {
}
hideIndex(){
this.renderer.addClass(this.buttons.nativeElement, "d-none");
}
showIndex(){
this.renderer.removeClass(this.buttons.nativeElement, "d-none");
}
title = '';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.