我正在开发一个以Angular 17编辑用户数据的简单对话框.问题是,当我try 从父组件注入数据时,VSCode中显示一个错误,内容如下:

没有合适的注入令牌用于UsersCrudmodalComponent类的参数‘Data’.考虑使用@Inject修饰符来指定注入令牌.

这是现在的代码:

家长:

editUser(uuid: string) {
    const user: IUser | undefined = this._users.find((f: IUser) => (f.uuid = uuid));
    const dialogRef = this.dialog.open(UsersCrudModalComponent, { data: { user: user } });
}

对话:

constructor(
    private readonly _dialogRef: MatDialogRef<LoginModalComponent>,
    private readonly _usersService: UsersService,
    private readonly _translate: TranslateService,
    @Inject(MAT_DIALOG_DATA) public data: { user: IUser },
) {
    this.user = this.data.user;
}

另一方面,如果我将@Inject行移动到构造函数注入的第一行,我会得到这个错误:

作为表达式调用时,无法解析参数修饰符的签名."0"类型的参数不能赋值给"3"类型的参数.

我怀疑这与打字本有关,但我看不出有什么奇怪的地方.

Tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true,
    "sourceMap": true,
    "declaration": false,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "module": "ES2022",
    "useDefineForClassFields": false,
    "lib": ["ES2022", "dom"]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

Anular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "gaia-spa": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss",
          "standalone": false
        },
        "@schematics/angular:directive": {
          "standalone": false
        },
        "@schematics/angular:pipe": {
          "standalone": false
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist/gaia-spa",
            "index": "src/index.html",
            "browser": "src/main.ts",
            "polyfills": ["zone.js"],
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": [
              "@angular/material/prebuilt-themes/pink-bluegrey.css",
              "src/styles.scss"
            ],
            "scripts": ["./node_modules/crypto-js/crypto-js.js"],
            "server": "src/main.server.ts",
            "prerender": true,
            "stylePreprocessorOptions": {
              "includePaths": ["src/assets"]
            },
            "ssr": {
              "entry": "server.ts"
            }
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all",
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.dev.ts"
                }
              ]
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "buildTarget": "gaia-spa:build:production"
            },
            "development": {
              "buildTarget": "gaia-spa:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "buildTarget": "gaia-spa:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": ["zone.js", "zone.js/testing"],
            "tsConfig": "tsconfig.spec.json",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": [
              "@angular/material/prebuilt-themes/pink-bluegrey.css",
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "cli": {
    "analytics": "(private)"
  }
}

Package.json

{
  "name": "gaia-spa",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "start:dev": "ng serve --configuration development",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "serve:ssr:gaia-spa": "node dist/gaia-spa/server/server.mjs"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^17.0.0",
    "@angular/cdk": "^17.0.5",
    "@angular/common": "^17.0.0",
    "@angular/compiler": "^17.0.0",
    "@angular/core": "^17.0.0",
    "@angular/forms": "^17.0.0",
    "@angular/material": "^17.0.3",
    "@angular/platform-browser": "^17.0.0",
    "@angular/platform-browser-dynamic": "^17.0.0",
    "@angular/platform-server": "^17.0.0",
    "@angular/router": "^17.0.0",
    "@angular/ssr": "^17.0.5",
    "@fortawesome/angular-fontawesome": "^0.14.1",
    "@fortawesome/fontawesome-svg-core": "^6.5.1",
    "@fortawesome/free-solid-svg-icons": "^6.5.1",
    "@ngx-translate/core": "^15.0.0",
    "@ngx-translate/http-loader": "^8.0.0",
    "@types/crypto-js": "^4.2.1",
    "crypto-js": "^4.2.0",
    "express": "^4.18.2",
    "intl": "^1.2.5",
    "ngx-bootstrap": "^6.2.0",
    "ngx-webstorage": "^13.0.1",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.14.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^17.0.5",
    "@angular/cli": "^17.0.5",
    "@angular/compiler-cli": "^17.0.0",
    "@angular/localize": "^17.0.5",
    "@types/core-js": "^2.5.8",
    "@types/express": "^4.17.17",
    "@types/jasmine": "~5.1.0",
    "@types/node": "^18.18.0",
    "jasmine-core": "~5.1.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.2.2"
  }
}

I modified the Tsconfig.json, changing experimentalDecorators from true to false. Now the first error doesn't appear anymore, but I still have the "没有合适的注入令牌用于UsersCrudmodalComponent类的参数‘Data’.考虑使用@Inject修饰符来指定注入令牌."

推荐答案

在try 更改类型加密和 node 版本之后,我放弃了,最终try 返回到最后一次提交,再次下载NODE_MODULES,现在它没有显示任何错误.

此外,我必须说,根据官方文件,我没有一个很好的版本的Typescript 和 node 的Angular 17.

我现在的代码是这样的:

Package.json

{
  "name": "gaia-spa",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "start:dev": "ng serve --configuration development",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "serve:ssr:gaia-spa": "node dist/gaia-spa/server/server.mjs"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^17.0.0",
    "@angular/cdk": "^17.0.3",
    "@angular/common": "^17.0.0",
    "@angular/compiler": "^17.0.0",
    "@angular/core": "^17.0.0",
    "@angular/forms": "^17.0.0",
    "@angular/material": "^17.0.3",
    "@angular/platform-browser": "^17.0.0",
    "@angular/platform-browser-dynamic": "^17.0.0",
    "@angular/platform-server": "^17.0.0",
    "@angular/router": "^17.0.0",
    "@angular/ssr": "^17.0.5",
    "@fortawesome/angular-fontawesome": "^0.14.1",
    "@fortawesome/fontawesome-svg-core": "^6.5.1",
    "@fortawesome/free-solid-svg-icons": "^6.5.1",
    "@ngx-translate/core": "^15.0.0",
    "@ngx-translate/http-loader": "^8.0.0",
    "@types/crypto-js": "^4.2.1",
    "crypto-js": "^4.2.0",
    "express": "^4.18.2",
    "intl": "^1.2.5",
    "ngx-bootstrap": "^6.2.0",
    "ngx-webstorage": "^13.0.1",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.14.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^17.0.5",
    "@angular/cli": "^17.0.5",
    "@angular/compiler-cli": "^17.0.0",
    "@angular/localize": "^17.0.5",
    "@types/core-js": "^2.5.8",
    "@types/express": "^4.17.17",
    "@types/jasmine": "~5.1.0",
    "@types/node": "^18.18.0",
    "jasmine-core": "~5.1.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.2.2"
  }
}

Tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true,
    "sourceMap": true,
    "declaration": false,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "module": "ES2022",
    "useDefineForClassFields": false,
    "lib": ["ES2022", "dom"]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

Angular相关问答推荐

日语字符不受角形约束控制

角部炎问题

如何使用解析器处理Angular 路由中存储的查询参数以避免任何额外的导航?

bootstrap 可折叠菜单在单击时未展开.Angular 应用程序

带区域设置用法的Angular 日期管道

对Angular 为17的路径使用AuthGuard

显示用户名-Angular

VSCode 调试器空白页面并在使用 VS Code 1.76.1 和 Chrome 111 启动时加载

执行ng generate environments时出错时需要合集和原理图

Angular 测试被认为是成功的,即使有错误

如何更改 Angular material 按钮中的内部 RippleColor 和 UnboundedRipple 效果?

找不到模块'webpack'

如何在数据表angular material中显示空消息,如果未找到数据

如何在插值中编写条件?

Angular 5 中服务的生命周期是什么

请添加@Pipe/@Directive/@Component 注解

初始化所有子类后的Angular 2生命周期钩子是什么?

Angular2中是否有像window.onbeforeunload这样的生命周期钩子?

需要选中一个复选框

如何在 Angular CLI 6: angular.json 中添加 Sass 编译?