如何修复“错误:StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]”
Posted
技术标签:
【中文标题】如何修复“错误:StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]”【英文标题】:How to fix "Error: StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]" 【发布时间】:2019-09-08 20:40:35 【问题描述】:我正在尝试使用 Angular 创建一个 web 应用程序,该项目编译时没有错误。执行时浏览器显示一个空白页面并返回此错误:
错误错误:未捕获(承诺中):错误:StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]:
StaticInjectorError(Platform: core)[NgbDropdown -> ChangeDetectorRef]:
NullInjectorError: 没有 ChangeDetectorRef 的提供者!
错误:StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]:
StaticInjectorError(Platform: core)[NgbDropdown -> ChangeDetectorRef]:
NullInjectorError: 没有 ChangeDetectorRef 的提供者!
在 NullInjector.push../node_modules /@angular/core/fesm5/core.js.NullInjector.get(core.js:8896)
在resolveToken(core.js: 9141)
在 tryResolveToken(core.js: 9085)
在 StaticInjector.push../ node_modules /@angular/core/fesm5 / core.js.StaticInjector.get(core.js: 8982)
在resolveToken(core.js: 9141)
在 tryResolveToken(core.js: 9085)
在 StaticInjector.push../ node_modules /@angular/core/fesm5 / core.js.StaticInjector.get(core.js: 8982)
在 resolveNgModuleDep(core.js: 21218)
在 NgModuleRef_.push../ node_modules /@angular/core/fesm5 / core.js.NgModuleRef_.get(core.js: 21907)
在 resolveDep(core.js: 22278)
在 resolvePromise(zone.js: 831)
在 resolvePromise(zone.js: 788)
在 zone.js: 892
在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask(zone.js:423)
在 Object.onInvokeTask(core.js: 17290)
在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask(zone.js:422)
在 Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask(zone.js:195)
在 drainMicroTaskQueue(zone.js: 601)
那是我的 app.module.ts:
import BrowserAnimationsModule from '@angular/platform-browser/animations';
import NgModule from '@angular/core';
import FormsModule from '@angular/forms';
import HttpClientModule from '@angular/common/http';
import RouterModule from '@angular/router';
import NgbModule from '@ng-bootstrap/ng-bootstrap';
import ToastrModule from 'ngx-toastr';
import AppRoutingModule from './app-routing.module';
import ComponentsModule from './components/components.module';
import AppComponent from './app.component';
import AdminLayoutComponent from './layouts/admin-layout/admin-layout.component';
@NgModule(
imports: [
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
ComponentsModule,
RouterModule,
AppRoutingModule,
NgbModule,
ToastrModule.forRoot()
],
declarations: [
AppComponent,
AdminLayoutComponent
],
providers: [],
bootstrap: [AppComponent]
)
export class AppModule
这就是我的 package.json:
"name": "test-project",
"version": "0.0.0",
"scripts":
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
,
"private": true,
"dependencies":
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
,
"devDependencies":
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.8",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
我的app.component.ts
import Component from '@angular/core';
@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)
export class AppComponent
title = 'testProject';
和admin-layout.component.ts
import filter from 'rxjs/operators';
import Component, OnInit, ViewChild, AfterViewInit from '@angular/core';
import Location, LocationStrategy, PathLocationStrategy, PopStateEvent from '@angular/common';
import NavbarComponent from '../../components/navbar/navbar.component';
import Router, NavigationEnd, NavigationStart from '@angular/router';
import Subscription, Observable from 'rxjs';
import PerfectScrollbar from 'perfect-scrollbar';
@Component(
selector: 'app-admin-layout',
templateUrl: './admin-layout.component.html',
styleUrls: ['./admin-layout.component.css']
)
export class AdminLayoutComponent implements OnInit
private _router: Subscription;
private lastPoppedUrl: string;
private yScrollStack: number[] = [];
constructor(public location: Location, private router: Router)
ngOnInit()
const isWindows = navigator.platform.indexOf('Win') > -1 ? true : false;
if (isWindows && !document.getElementsByTagName('body')[0].classList.contains('sidebar-mini'))
// if we are on windows OS we activate the perfectScrollbar function
document.getElementsByTagName('body')[0].classList.add('perfect-scrollbar-on');
else
document.getElementsByTagName('body')[0].classList.remove('perfect-scrollbar-off');
const elemMainPanel = <HTMLElement>document.querySelector('.main-panel');
const elemSidebar = <HTMLElement>document.querySelector('.sidebar .sidebar-wrapper');
this.location.subscribe((ev: PopStateEvent) =>
this.lastPoppedUrl = ev.url;
);
this.router.events.subscribe((event: any) =>
if (event instanceof NavigationStart)
if (event.url != this.lastPoppedUrl)
this.yScrollStack.push(window.scrollY);
else if (event instanceof NavigationEnd)
if (event.url == this.lastPoppedUrl)
this.lastPoppedUrl = undefined;
window.scrollTo(0, this.yScrollStack.pop());
else
window.scrollTo(0, 0);
);
this._router = this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: NavigationEnd) =>
elemMainPanel.scrollTop = 0;
elemSidebar.scrollTop = 0;
);
if (window.matchMedia(`(min-width: 960px)`).matches && !this.isMac())
let ps = new PerfectScrollbar(elemMainPanel);
ps = new PerfectScrollbar(elemSidebar);
ngAfterViewInit()
this.runOnRouteChange();
isMaps(path)
var titlee = this.location.prepareExternalUrl(this.location.path());
titlee = titlee.slice(1);
if (path == titlee)
return false;
else
return true;
runOnRouteChange(): void
if (window.matchMedia(`(min-width: 960px)`).matches && !this.isMac())
const elemMainPanel = <HTMLElement>document.querySelector('.main-panel');
const ps = new PerfectScrollbar(elemMainPanel);
ps.update();
isMac(): boolean
let bool = false;
if (navigator.platform.toUpperCase().indexOf('MAC') >= 0 || navigator.platform.toUpperCase().indexOf('IPAD') >= 0)
bool = true;
return bool;
【问题讨论】:
【参考方案1】:NullInjectorError:没有 ChangeDetectorRef 的提供者!
告诉你需要在你的组件中导入changeDetectorRef:
import ChangeDetectorRef from '@angular/core'
然后在你的构造函数中:
constructor(private changeDetectorRef: ChangeDetectorRef)
然后你可以在你的组件中使用它:this.changeDetectorRef...
【讨论】:
是否必须将这两行代码添加到 app.module.ts 文件中? 您将这两行都添加到组件中。所以无论是 AppComponent 还是 AdminLayoutComponent 另外它也不需要在 app.module 中以上是关于如何修复“错误:StaticInjectorError(AppModule)[NgbDropdown -> ChangeDetectorRef]”的主要内容,如果未能解决你的问题,请参考以下文章