我正在尝试使用Angular创建一个webapp,该项目的编译没有错误。执行后,浏览器显示空白页面并返回此错误:
错误错误:未被捕获(承诺):错误:StaticInjectorError(AppModule)[NgbDropdown-> ChangeDetectorRef]:
StaticInjectorError(平台:核心)[NgbDropdown-> ChangeDetectorRef]:
NullInjectorError:没有ChangeDetectorRef的提供者!
错误:StaticInjectorError(AppModule)[NgbDropdown-> ChangeDetectorRef]:
StaticInjectorError(平台:核心)[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)
在排水微任务队列(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;
}
}
答案 0 :(得分:0)
NullInjectorError:没有ChangeDetectorRef的提供者!
告诉您您需要在组件中导入changeDetectorRef:
import { ChangeDetectorRef } from '@angular/core'
然后在您的构造函数中:
constructor(private changeDetectorRef: ChangeDetectorRef){}
然后可以在组件中使用它,例如:this.changeDetectorRef...