gpt4 book ai didi

angular - 空注入(inject)器错误 : No provider for ViewportScroller

转载 作者:太空狗 更新时间:2023-10-29 16:57:02 26 4
gpt4 key购买 nike

我正在构建一个简单的 Angular 应用程序,使用 Angular Material 来设计前端。该应用程序只有 2 个组件呈现在同一页面中。当我为应用程序提供服务时,在控制台中返回:

NullInjectorError: "StaticInjectorError(AppModule)[RouterScroller -> ViewportScroller]: StaticInjectorError(平台:核心)[RouterScroller -> ViewportScroller]: NullInjectorError:没有 ViewportScroller 的提供者!”

AppModule.ts

          import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { Material } from './Material';
import { DataFormComponent } from './data-form/data-form.component';
import { TemplateFormComponent } from './template-form/template-form.component';
import { FormsModule } from '@angular/forms';


@NgModule({
declarations: [
AppComponent,
DataFormComponent,
TemplateFormComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
Material,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

应用组件.ts

import { Component } from '@angular/core';

Material.ts(所有 Angular Material 组件的模块)

                import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {NgModule} from '@angular/core';
import {MatTabsModule} from '@angular/material/tabs';
import {MatInputModule} from '@angular/material';
import {MatCardModule} from '@angular/material/card';
import {MatToolbarModule} from '@angular/material/toolbar';
import {MatFormFieldModule} from '@angular/material/form-field';
import {MatButtonModule} from '@angular/material/button';

@NgModule({
imports:
[
BrowserAnimationsModule,
MatTabsModule,
MatCardModule,
MatToolbarModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule
],
exports:
[
BrowserAnimationsModule,
MatTabsModule,
MatCardModule,
MatToolbarModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule
],
})
export class Material { }

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'forms';
}

AppRoutingModule.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CursoModule } from './curso/curso.module';

const routes: Routes = [
{
path: '', pathMatch: 'full', redirectTo: 'curso'
},
{
path: 'curso', loadChildren: './curso/curso.module#CursoModule'
}
];

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

cursp-routing.module.ts:

import { CursosComponent } from './cursos.component';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CursoListaComponent } from './curso-lista/curso-lista.component';

const routes = [
{
path: '', component: CursoListaComponent
}
]


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

最佳答案

我有同样的错误 - 在项目中更新 Angular 对我有用(这是一个只有 Angular Material、几个简单组件和简单路由的新项目)。

ng update @angular/cli @angular/core

https://update.angular.io/#7.2:8.0

我做后出现的错误:

npm install -S @angular/material @angular/cdk @angular/animations
npm uninstall @angular/core
npm install -S @angular/core

作为错误的解决方案“在'@angular/core'中找不到导出'ɵɵinject'(在将Angular Material添加到全新项目后抛出)。

它一直有效,直到我添加路由并得到 NullInjectorError: No provider for ViewportScroller。但正如开头所述 - 将整个项目更新为 Angular v8 似乎是解决方案。

关于angular - 空注入(inject)器错误 : No provider for ViewportScroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56879099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com