angular - 在页面之间导航时未调用 ngAfterViewInit

标签 angular ionic-framework angular6 ionic4

购物车页面

import { Component, OnDestroy, AfterViewInit, OnInit, AfterContentInit, ChangeDetectorRef, } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router'
import { StorageService } from '../data/storage.service';
import { DataProviderService } from '../data/data-provider.service';
import * as _ from 'lodash';
@Component({
  selector: 'app-cart',
  templateUrl: './cart.page.html',
  styleUrls: ['./cart.page.scss'],
})
export class CartPage implements AfterViewInit, AfterContentInit, OnDestroy, OnInit {

  cartItems = [];
  totalAmount = 0;

  constructor(public cart: StorageService, private cdRef: ChangeDetectorRef, public dataProvider: DataProviderService, private router: Router, private route: ActivatedRoute) {
  }



  ngOnInit() {

    console.log("Init callled")
  }

  async ngAfterViewInit() {
    console.log("Lets check!!");
    console.log("\n\n\n\nthis is init func");
  }
}

详情页

import { Component, OnInit, OnDestroy, AfterViewInit, ChangeDetectorRef, ChangeDetectionStrategy, AfterContentInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DataProviderService } from '../data/data-provider.service';
import { UtilsService } from '../data/utils.service';
import { StorageService } from '../data/storage.service';
import { Router, NavigationEnd } from '@angular/router'
import * as _ from 'lodash';

@Component({
  selector: 'app-details',
  templateUrl: './details.page.html',
  styleUrls: ['./details.page.scss'],
})
export class DetailsPage implements AfterViewInit, AfterContentInit, OnDestroy {
  constructor(private route: ActivatedRoute, private cdRef: ChangeDetectorRef, public dataProvider: DataProviderService, public utils: UtilsService, public cart: StorageService, private router: Router) {

  }

  ngAfterViewInit() {
   console.log("Details page view Init");
  }
  ngOnDestroy() {
    console.log("I'm destroying details!!");
  }
  ngAfterContentInit() {
    console.log("View content is triggered");
  }

路由器模块

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadChildren: './home/home.module#HomePageModule' },
  { path: 'details/:category/:id', loadChildren: './details/details.module#DetailsPageModule',runGuardsAndResolvers: 'always'},
  { path: 'products/:category', loadChildren: './products/products.module#ProductsPageModule' },
  { path: 'products/:category/:market', loadChildren: './products/products.module#ProductsPageModule' },
  { path: 'reports', loadChildren: './reports/reports.module#ReportsPageModule' },
  { path: 'news', loadChildren: './news/news.module#NewsPageModule' },
  { path: 'buy', loadChildren: './buy/buy.module#BuyPageModule' },
  { path: 'Offers', loadChildren: './offers/offers.module#OffersPageModule' },
  { path: 'cart', loadChildren: './cart/cart.module#CartPageModule' , runGuardsAndResolvers: 'always'},
  { path: 'checkout', loadChildren: './checkout/checkout.module#CheckoutPageModule' },
];

@NgModule({
  imports: [RouterModule.forRoot(routes,{onSameUrlNavigation: 'reload'})],
  exports: [RouterModule]
})
export class AppRoutingModule { }

当我第一次加载页面时,它会调用函数 ngAfterViewInit。但是当我导航到其他路线并返回时,ngAfterViewInit 没有被调用。

如果我们重新加载页面,

ngOnInit 将不会被调用。所以我正在使用 ngAfterViewInit 这没有帮助。这是预期的行为吗?

我如何在每次用户导航到页面时执行一个函数?

最佳答案

关于angular - 在页面之间导航时未调用 ngAfterViewInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55205245/

相关文章:

javascript - 如何将此 AngularJS HTML 模板添加到我的 Angular 8 项目中

angular - Ionic Angular TypeError : architect_1. createBuilder 不是函数

ios - IONIC 应用程序未在 iPhone 设备 Xcode 10 中运行

javascript - Ionic 中的 $state.go 与 ion-nav-view 不起作用

angular - 在 Angular 6 中顺序执行 http 请求

angular-material - ng 生成 @angular/material :material-nav --name=main-nav is not working

javascript - Angular 5 和转换仅在 'new' 项中

angular - Angular 日期前管道吗?

angular - Dotnet Angular 模板和 Core 2.0 MVC

php - Angular 6 HttpClient 和 PHP 错误 (unknown url) : 0 Unknown Error 的 Http 失败响应