【发布时间】:2020-05-25 06:34:40
【问题描述】:
我对堆栈完全陌生,我在使用 Angular 的材料模块时遇到了一些问题。我正在尝试在我的代码中导入“@angular/material”模块,但每当我导入它时都会出错。错误如下:
ERROR in src/app/app.module.ts:5:32 - error TS2306: File '/Users/anmolsarraf/Desktop/MEAN Stack/mean-course/node_modules/@angular/material/index
.d.ts' is not a module.
这是我的 package.json 文件:
{
"name": "mean-course",
"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/animation": "^4.0.0-beta.8",
"@angular/animations": "~8.2.7",
"@angular/cdk": "^9.0.0",
"@angular/common": "~8.2.7",
"@angular/compiler": "~8.2.7",
"@angular/core": "~8.2.7",
"@angular/forms": "~8.2.7",
"@angular/material": "^9.0.0",
"@angular/platform-browser": "~8.2.7",
"@angular/platform-browser-dynamic": "~8.2.7",
"@angular/router": "~8.2.7",
"rxjs": "~6.4.0",
"tslib": "^1.10.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.5",
"@angular/cli": "~8.3.5",
"@angular/compiler-cli": "~8.2.7",
"@angular/language-service": "~8.2.7",
"@types/node": "~8.9.4",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.5.3"
}
}
这是我的 app.module.ts 文件:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatInputModule } from '@angular/material/input';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { PostCreateComponent } from './posts/post-create/post-create.component';
@NgModule({
declarations: [
AppComponent,
PostCreateComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
BrowserAnimationsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
确切地说,我正在尝试执行import { MatInputModule } from '@angular/material';,但遇到了上述错误。
我尝试将上述模块导入为import { MatInputModule } from '@angular/material/input';,但随后它会抛出一堆错误,说node_modules/@angular/material/input/input.d.ts:138:9 - error TS1086: An accessor cannot be declared in an ambient context.
任何帮助将不胜感激。谢谢!
更新
我创建了一个 Angular 应用程序的新实例,然后尝试在其中导入 Angular 材质,但它以某种方式工作,因为我在导入它时没有遇到任何错误。谢谢!
【问题讨论】:
-
你把
npm install添加到你的项目后运行了吗? -
是的,我在将它添加到我的项目后也执行了该命令,但没有成功:-(
-
同时发布您的导入声明
-
我已经在问题中添加了上面的导入语句。
-
这可能是因为
@angular/material是 v9 和 Angular v8,但不完全确定。你能确定 Angular 和 Angular Material 的版本是一样的吗? (删除 node_modules,重新安装等)
标签: javascript node.js angular angular-material mean-stack