【问题标题】:ngOninit hook of AppComponent is not being called on Page refresh页面刷新时未调用 AppComponent 的 ngOninit 钩子
【发布时间】:2020-09-11 17:53:23
【问题描述】:

我导航到/posts (localhost:4200/posts),但是当我刷新页面时AppComponentngOnInit() 没有被调用。

【问题讨论】:

  • 你能在 stackblitz 中重现这个问题吗?将您的代码添加到您的问题中可能会帮助其他人重现问题。
  • 您的路由器插座在哪里。你甚至会在路由器插座中显示 AppComponent 吗?

标签: angular typescript


【解决方案1】:
import { Component, OnInit } from '@angular/core';

export class App implements OnInit {
  constructor() {
     // Called first time before the ngOnInit()
  }

  ngOnInit() {
     // Called after the constructor and called  after the first ngOnChanges() 
  }
}
is this the same way you have implemented in your project?

【讨论】:

  • 是的,它与我实现它的方式相同。但我很惊讶地看到页面重新加载时没有调用 ngOninit
  • 您可以尝试在构造函数本身中调用该服务吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
相关资源
最近更新 更多