【问题标题】:error TS1086: An accessor cannot be declared in an ambient context in Angular 9错误 TS1086:无法在 Angular 9 的环境上下文中声明访问器
【发布时间】:2020-05-24 15:29:56
【问题描述】:

我正在学习 Angular Material,从“@angular/material/button”导入 { MatButtonModule } 时出现此错误。

从我在其他答案中看到的内容来看,它看起来像是包兼容性问题,但我无法修复它。

这是完整的错误

ERROR in node_modules/@angular/cdk/a11y/focus-trap/focus-trap.d.ts(29,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/focus-trap/focus-trap.d.ts(30,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/focus-trap/focus-trap.d.ts(128,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/focus-trap/focus-trap.d.ts(129,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/focus-trap/focus-trap.d.ts(134,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/focus-trap/focus-trap.d.ts(135,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/key-manager/list-key-manager.d.ts(96,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/key-manager/list-key-manager.d.ts(98,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/live-announcer/live-announcer.d.ts(69,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/a11y/live-announcer/live-announcer.d.ts(70,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/observers/observe-content.d.ts(62,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/observers/observe-content.d.ts(63,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/observers/observe-content.d.ts(66,9): error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@angular/cdk/observers/observe-content.d.ts(67,9): error TS1086: An accessor cannot be declared in an ambient context.

这是我的 package.json

"name": "football",
  "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": "~8.2.14",
    "@angular/cdk": "^9.0.0",
    "@angular/common": "~8.2.14",
    "@angular/compiler": "~8.2.14",
    "@angular/core": "~8.2.14",
    "@angular/forms": "~8.2.14",
    "@angular/material": "8.2.3",
    "@angular/platform-browser": "~8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.14",
    "@angular/router": "~8.2.14",
    "hammerjs": "^2.0.8",
    "rxjs": "~6.4.0",
    "tslib": "^1.10.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.21",
    "@angular/cli": "~8.3.21",
    "@angular/compiler-cli": "~8.2.14",
    "@angular/language-service": "~8.2.14",
    "@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"
  }
}

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    根据您的 package.json,您使用的是 Angular 8.3,但您已导入 angular/cdk v9。您可以降级 Angular/cdk 版本,也可以通过运行将 Angular 版本升级到 v9:

    ng update @angular/core @angular/cli

    这会将您的本地 Angular 版本更新为 9。然后,为了同步素材,运行: ng update @angular/material

    【讨论】:

    • 谢谢,我在安装 angular flex 时遇到了这个问题,它自动安装了最新版本(在我的例子中是版本 9),但我使用的是 angular 8。更正版本解决了这个问题
    • @Kyler johnson 我如何降低 angular/cdk
    • @mdkamrul 你可以通过运行npm i @angular/core@8 @angular/cli@8 @angular/material@8 来降级,假设你使用的是版本 8。有时你的 package.json 和 package-lock.json 会不同步,你的项目会使用里面的内容package-lock.json 文件。所以现在,运行npm i 来同步它们。如果之后仍然遇到问题,请运行 npm ci 以完全干净地安装您的依赖项
    • 我不得不使用ng update @angular/cli @angular/core --force,因为软件包本身不兼容,否则您无法切换。
    【解决方案2】:

    首先请在module.ts 文件中检查@NgModule 中的所有属性都只有一次。 如果其中任何一个不止一次,那么也会出现此错误。 因为我也发生了这个错误,但是在module.ts 文件中entryComponents 属性是两次,这就是我收到这个错误的原因。 我通过从@NgModule 中删除一次entryComponents 解决了这个错误。 所以,我建议你先好好检查一下。

    【讨论】:

      【解决方案3】:

      compilerOptions 内的tsconfig.json 文件中添加skipLibCheck: true 解决了我的问题。

      "compilerOptions": {
         "skipLibCheck": true,
      },
      

      【讨论】:

      • 但是,将来跳过lib检查不会导致其他问题吗?
      • 感谢@R15 它对我有用,我正在使用 angular 8
      • 你绝对的传奇。这是一种享受。我担心使用更新打字稿的其他修复,以防我的 Angular 版本不兼容......
      • 效果非常好...非常感谢您节省了我的时间
      【解决方案4】:

      这些问题通常是由于 @ngx-translate/core 版本和 Angular 不匹配造成的。在安装之前检查相应 ngx_trnalsate/Core 的兼容版本,@ngx-translate /http-loader 和 Angular https://www.npmjs.com/package/@ngx-translate/core

      例如:对于 Angular 6.X 版本,

      npm install @ngx-translate/core@10 @ngx-translate/http-loader@3 rxjs --save
      

      和上面一样,遵循下面的命令,其余代码部分对所有版本都是通用的(注意:版本可以从(https://www.npmjs.com/package/@ngx-translate/core)获取

      npm install @ngx-translate/core@version @ngx-translate/http-loader@version rxjs --save
      

      【讨论】:

        【解决方案5】:

        我遇到了这个问题,但我的 package.json 中没有版本冲突。

        我的 package-lock.json 不知何故与 package json 不同步。删除和重新生成它对我有用。

        【讨论】:

          【解决方案6】:

          我通过以下步骤解决了同样的问题:

          检查角度版本:使用命令:ng version 我的角度版本是:Angular CLI:7.3.10

          之后,我通过以下链接获得了 ngx bootstrap 的支持版本:https://www.npmjs.com/package/ngx-bootstrap

          在 package.json 文件中更新版本:“bootstrap”:“^4.5.3”,“@ng-bootstrap/ng-bootstrap”:“^4.2.2”,

          现在更新 package.json 后,使用命令 npm update

          在这个使用命令 ng serve 之后,我的错误得到了解决

          【讨论】:

            猜你喜欢
            • 2021-07-09
            • 2020-05-22
            • 2020-03-07
            • 1970-01-01
            • 2020-07-21
            • 2020-07-09
            • 1970-01-01
            相关资源
            最近更新 更多