angular 子路由跳转出现Navigation triggered outside Angular zone, did you forget to call ‘ngZone.run() 的问题修复

angular 路由功能非常强大,同时angular的路由也非常脆弱,非常容易出现错误。

那么在我们遇到异常时,首先要做的是什么?

第一步:检查代码,对比官方文档,发现代码中不一致的地方进行改正。

第二步:调试代码,观察调用过程中参数传递是否正常。

第三步:百度一下。

对于我这个观点,可能会有人不服气,出现异常不应该第一时间问度娘吗?对于较熟悉angular 路由的人来说,这确实是一个快速的解决问题之道,但不是我们学习的根本。我们学习要知根知底。

对于angular的子路由,我们来看一个例子

import { NgModule } from ‘@angular/core‘;
import { Routes, RouterModule } from ‘@angular/router‘;
import { Leftlistzuhezuheleftlistform1030RootComponent } from ‘./components/root-component/rootcomponent‘;
import { RootComponent as zuheRight2Form1030RootComponent } from ‘../../../../zuheright2form1030/components/root-component/rootcomponent‘;

const routes: Routes = [
  {
    path: ‘‘, component: Leftlistzuhezuheleftlistform1030RootComponent,
    children: [{
      path: ‘KK/:id/:name‘
      , component: zuheRight2Form1030RootComponent
    }
    ]
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(routes)
  ],
  exports: [
    RouterModule
  ]
})
export class Leftlistzuhezuheleftlistform1030zuheleftListForm1030RoutingModule { }

我们如何进行路由跳转呢,我们一般通过路由组件的navigate方法,而不是通过超链接,毕竟通过代码可以自定义变量、自定义url、自定义一系列东东。

import { Injectable } from ‘@angular/core‘;
import { Router, RouterModule, ActivatedRoute } from ‘@angular/router‘;
@Injectable()
export class ZuheleftListForm1030FrmVfvfvfService {
  constructor(        private router: Router,
    private route: ActivatedRoute) { }

  dk() {
    console.log("执行路由跳转");
   this.router.navigate(["right1"],{relativeTo:this.route});
  }
}

当我们通过我们自定义的方法进行路由跳转时,我们看到控制台输出了 执行路由跳转字样,说明这个方法被调用了,但是同时我们在控制台也看到了一个错误:Navigation triggered outside Angular zone, did you forget to call ‘ngZone.run() 。虽然该错误并没有被标记成error但我们仍要解决下,否则路由跳转会出现让我们意想不到的结果。

如何解决该问题呢?通过分析我们代码,我们发现我们自定义的方法并没有包含在angular 的ngZone中,那么什么是ngZone呢。

了解了一系列的原因,解决该问题就比较简单了,从ngZone中去执行路由跳转,即可。

处理方式:

import { Injectable,NgZone } from ‘@angular/core‘;
import { Router, RouterModule, ActivatedRoute } from ‘@angular/router‘;
@Injectable()
export class ZuheleftListForm1030FrmVfvfvfService {
    constructor(
      private router: Router,
      private route: ActivatedRoute,
      private ngZone: NgZone,

    ) { }

    dk()
    {
      console.log("执行路由跳转");
      this.ngZone.run(()=>{
        this.router.navigate(["kk"],{relativeTo:this.route}).then();
      });
  }
}

angular 路由跳转经常出现找不到匹配Url的问题,针对这个问题,我们首选的方案是将子路由相对当前路由进行跳转。也就是我们在执行

  this.router.navigate(["kk"],{relativeTo:this.route}).then();
加上relativeTo的原因。相对当前路由进行跳转,可以最大限度的减少路由地址不匹配的问题。

angular 路由需要我们持续的去审阅,多尝试,多阅读,必然了然于心。

原文地址:https://www.cnblogs.com/jiagoushi/p/11770397.html

时间: 2024-08-30 04:25:42

angular 子路由跳转出现Navigation triggered outside Angular zone, did you forget to call ‘ngZone.run() 的问题修复的相关文章

angular的路由跳转,的监听$rootScope.$on

使用angular来做项目时,习惯性的使用第三方路由插件ui-router配置路由.每一个状态都对应着一个页面, 因此对路由状态改变的监听也变的十分重要. 可以使用:$rootScope.$on(--.)监听 $stateChangeStart: 表示状态切换开始 $stateNoFound:没有发现 $stateChangeSuccess:切换成功 $stateChangeError:切换失败 回调函数的参数解释: event:当前事件的信息 toState:目的路由状态 toParams:传

Angular05 angular架构、搭建angular开发环境、组件必备三要素、angular启动过程

1 angular架构 1.1 组件:是angular应用的基本构建模块,可以理解为一段带有业务逻辑和数据的HTML 1.2 服务:用来封装可重用的业务逻辑 1.3 指令:允许你想HTML元素添加自定义功能 1.4 模块:将应用中的不同部分组织成一个angular框架可以理解的单元 1.5 组件+服务+指令 = 模块 组件+服务+指令 是用来完成业务功能的:模块 是用来打包和分发的 2 开发环境搭建 2.1 安装node.js 很简单,百度即可 安装完后在我们的命令窗口中就可以使用 npm 命令

[Angular] Create a custom validator for reactive forms in Angular

Also check: directive for form validation User input validation is a core part of creating proper HTML forms. Form validators not only help you to get better quality data but they also guide the user through your form. Angular comes with a series of

[Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular

By default the new Angular Http client (introduced in v4.3.1) uses JSON as the data format for communicating with the backend API. However, there might be situations where you may want to use some other format, like text/plain for fetching a CSV file

【Angular专题】——(2)【译】Angular中的ForwardRef

原文地址:https://blog.thoughtram.io/angular/2015/09/03/forward-references-in-angular-2.html 作者:Christoph Burgdorf 译者注:文章内容比较老,控制台信息等与新框架不完全一致,理解思路即可. 一. 问题点在哪里 先做一个小声明,我们现在拥有一个AppComponent,并使用DI系统向其中注入了一个NameService,因为我们使用的是Typescript,所以需要做的工作就是在构造函数的参数中

[Angular] Show a Loading Indicator for Lazy Routes in Angular

We can easily code split and lazy load a route in Angular. However when the user then clicks that lazy loaded route, it make some time to actually fetch and run the JavaScript bundle. In this lesson we'll see how we can implement a loading indicator

angular中的子路由用法

Angular ui-route的用法 引入angular和使用angular子路由时需要的依赖模块angular-ui-route.js.并且在html中将路由要插入的位置写上.而在js部分中和angular路由一样在控制台外面写 <body> <div ui-view></div> </body> <script src="js/angular.min.js" type="text/javascript" c

Android Jetpack - 使用 Navigation 管理页面跳转

在今年的 IO 大会上,发布了一套叫 Android Jetpack 的程序库.Android Jetpack 里的组件大部分我们都接触过了,其中也有一些全新的组件,其中一个就是 Navigation. 简介 Navigation 是用来管理 APP 里页面跳转的.起初,我以为它是用来代替 startActivity 的,但其实并不是,大家往下看就知道它的作用了. 另外,iOS 的同学可能会有似曾相识的感觉,Navigation 应该是有借鉴 Storyboard 的. 使用 我们先来看看 Na

[转贴]有关Angular 2.0的一切

对Angular 2.0的策略有疑问吗?就在这里提吧.在接下来的这篇文章里,我会解释Angular 2.0的主要特性区域,以及每个变化背后的动机.每个部分之后,我将提供自己在设计过程中的意见和见解,包括我认为仍然需要改进设计的重要部分. 注意:本文所反映是2014年11月6日的状态记录.如果你在较长时间之后读到此文,请检查一下我设计上是否有所变更. AngularJS 1.3 在开始讨论Angular的未来之前,我们先花点时间看看当前的版本.AngularJS 1.3是迄今为止最优的Angula