Search code examples
angularangular-materialangular17material-dialog

No suitable injection on Material Dialog


I'm working on a simple dialog to edit a user's data in Angular 17. The thing is that, when I try to inject data from the parent component, an error displays in VSCode that says:

No suitable injection token for parameter 'data' of class 'UsersCrudModalComponent'. Consider using the @Inject decorator to specify an injection token.

This is the code right now:

Parent:

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

Dialog:

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;
}

On the other hand, if I move the @Inject line to be the first one in the constructor injections, I get this error:

Unable to resolve signature of parameter decorator when called as an expression. Argument of type '0' is not assignable to parameter of type '3'.

I suspect it's related to typescript version, but I don't see anything odd.

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.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 "No suitable injection token for parameter 'data' of class 'UsersCrudModalComponent'. Consider using the @Inject decorator to specify an injection token."


Solution

  • After trying to change typescrypt and node versions, I gave up and ended up trying to go back to the last commit, download the node_modules again and now it doesn't show any error.

    Also, I must say I didn't have a good version of both typescript and node for Angular 17 according to the official documentation.

    My current code is like this right now:

    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
      }
    }