5

When adding the formbuilder in the constructor. I've been getting the error. I've added the ReactiveFormsModule in the app.module already.

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
@Component({
    selector: 'app',
    template: require('./app.component.pug'),
    styles: [require('./app.component.scss')]
})
export class AppComponent {
    private loginForm: FormGroup;
    constructor(private fb: FormBuilder) {}
}

Sample app.module.ts

import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
    imports: [BrowserModule, ReactiveFormsModule],
    declarations: [AppComponent],
    providers: [],
    bootstrap: [AppComponent]
})

Here is the error:

enter image description here

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
user8956707
  • 113
  • 8

2 Answers2

4

Added emitDecoratorMetadata: true to tsconfig.json file

user8956707
  • 113
  • 8
0

This works for me in a Stackblitz example.

Some comments though:

  • Why do you require the pug file as a template? If you are using pug (and pug-watch), the html file should be there as well.
  • why do you require the css? Just use simply styleUrls: [ './app.component.css' ] .
  • Are you sure your node, npm and angular version is up to date and compatible?

Edit: If removing the 'require' parts helps, try this:

@Component({
    selector: 'app',
    template: './app.component.html',
    styles: ['./app.component.scss']
})

There is no native pug support in angular yet, so you should rely on other tools to translate your page to a html file. In the @Component decorator you should try to stick to the official angular documentation. Personally, I use Gulp js for starting my dev app and a pug watcher at the same time. Here is my gulpfile:

const gulp = require('gulp');
const clean = require('gulp-clean');
const pug = require('gulp-pug');
const git = require('gulp-git');
const file = require('gulp-file');
const spawn = require('child_process').spawn;
const shell = require('gulp-shell');
const install = require('gulp-install');
const notify = require('gulp-notify');

// *********
// BUILD APP
// *********
gulp.task('npm-install', function () {
  gulp.src(['./package.json'])
    .pipe(install());
});
gulp.task('clean-dist', ['npm-install'], function () {
  return gulp.src('./dist', {read: false})
    .pipe(clean())
});
gulp.task('pug-build', function buildHTML() {
  return gulp.src('./src/**/*.pug')
    .pipe(pug({doctype: 'html', pretty: false}))
    .on('error', notify.onError(function (error) {
      return 'An error occurred while compiling pug.\nLook in the console for details.\n' + error;
    }))
    .pipe(gulp.dest('./src'))
});
gulp.task('ng-build', ['clean-dist', 'pug-build'], function (cb) {
  spawn('npm', ['run', 'ngbuild'], {stdio: 'inherit'}) 
});
gulp.task('build', ['ng-build'], function () {
});


// ****************
// DEVELOPMENT MODE
// ****************
gulp.task('pug-watch', ['pug-build'], function (cb) {
  gulp.watch('./src/**/*.pug', ['pug-build'])
});
gulp.task('ng-serve', function (cb) {
  spawn('ng', ['serve'], {stdio: 'inherit'});
});
gulp.task('dev-start', ['pug-watch', 'ng-serve']);

(in the package.json I have an entry:"ngbuild": "ng build --aot --progress=false" )

ForestG
  • 17,538
  • 14
  • 52
  • 86