简体   繁体   English

当我在 Angular 中单击后退按钮时,我无法导航到索引页面

[英]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中,确保已导入BrowserModuleAppRoutingModule

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.

相关问题 当我导航到 Angular 6 中的后页时如何清除下拉列表值 - How to clear the dropdowns values when i navigate to back page in Angular 6 使用 Angular 按下后退按钮时如何导航到另一条路线 - How do I navigate to another route when back button is pressed with Angular 我想在单击按钮时导航,但出现此错误 - I want to navigate when i click button but i got this error jhipster 过滤器结果正确返回,但是当我单击 UI 导航到结果集中的第 2 页时,我再次获得未过滤的结果 - jhipster filter results are returned correclty but when I click the UI to navigate to page 2 in the result set I get the unfiltered results back again 我试图创建一个设置页面,但是当我单击设置按钮导航到该页面时,控制台上出现以下上述错误 - I tried to make a settings page but when i click on settings button to navigate to that page ,the following above error is appearing on console 无法导航到Angular中的特定页面 - Cant navigate to specific page in Angular 当使用浏览器后退按钮导航到页面时,角度绑定在Chrome上的应用不正确 - Angular bindings applied incorrectly on Chrome when browser back button is used to navigate to page IONIC - ANGULAR 为什么单击返回按钮时我有相同的多个请求? - IONIC - ANGULAR Why when click back button I have same multiple request? Angular 2导航到浏览器上的主页,刷新按钮单击 - Angular 2 Navigate to home page on browser back,refresh button clicks 当我打开页面时,对Firebase的调用仅发生一次,而当我导航回它时则永远不会发生 - Call to Firebase happens only once when i open the page , then it never happens when i navigate back to it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM