質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Angular

Angularは、JavaScriptフレームワークです。AngularJSの後継であり、TypeScriptベースで実装されています。機能ごとに実装を分けやすく、コードの見通しが良いコンポーネント指向です。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

Q&A

1回答

3019閲覧

【Angular】Property '○○○' has no initializer and is not definitely assigned in the constructor. エラー

tonkotsu_ramen

総合スコア6

Angular

Angularは、JavaScriptフレームワークです。AngularJSの後継であり、TypeScriptベースで実装されています。機能ごとに実装を分けやすく、コードの見通しが良いコンポーネント指向です。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

0グッド

0クリップ

投稿2021/08/22 03:19

編集2021/08/22 08:23

【前提】

Angularの初学者です。typescript自体も初めてさわる段階です。
現在、書籍で学習しており、フォームのバリデーション を学習中なのですが、タイトルのエラーを解決できない状態です。下記のような症状ですが、どのような原因が考えられるかアドバイス頂けないでしょうか。宜しくお願い致します。

【開発環境】
angular:12
os:MAC

【現在の状況】

下記のようなエラーが出ている状態です。
(全ての)プロパティを定義している箇所でエラーがでています。内容としては、コンストラクタで初期化していないですよ言っているかと思われます。

【やってみたこと】

・ tsconfig.json"strictPropertyInitialization": falseを追記してみる。
⇨症状は全く変わらず。

・ string→anyに変えてみる
⇨とりあえずエラーは消えるが根本解決になっていない気がする。

・ title!: string;ようにプロパティの後ろに!をつけてみる。
⇨別のエラーが出る。(ややこしくなるのでひとまず割愛)

・ title: string | undefined;としてみる。
⇨別のエラーが出る。(ややこしくなるのでひとまず割愛)

参考にしたサイト
http://one-way.tech/programing/basic-typescript/
https://tech-up.hatenablog.com/entry/2019/03/15/152258

※エラーメッセージ ✔ Browser application bundle generation complete. Lazy Chunk Files | Names | Size src_app_registration_registration_module_ts.js | - | 12.29 kB 6 unchanged chunks Build at: 2021-08-22T02:56:59.731Z - Hash: c222efc48625979724c3 - Time: 728ms Error: src/app/registration/registration.component.ts:15:3 - error TS2564: Property 'title' has no initializer and is not definitely assigned in the constructor. 15 title: string; ~~~~~ Error: src/app/registration/registration.component.ts:16:3 - error TS2564: Property 'message' has no initializer and is not definitely assigned in the constructor. 16 message: string; ~~~~~~~ Error: src/app/registration/registration.component.ts:17:3 - error TS2564: Property 'myControl' has no initializer and is not definitely assigned in the constructor. 17 myControl: FormGroup; ~~~~~~~~~

コード

※基本的には教本のコピペです。

typescript

1//registration.component.ts 2import { Component, OnInit } from '@angular/core'; 3import { 4 FormControl, 5 FormGroup, 6 FormBuilder, 7 Validators, 8} from '@angular/forms'; 9 10@Component({ 11 selector: 'app-registration', 12 templateUrl: './registration.component.html', 13 styleUrls: ['./registration.component.scss'], 14}) 15export class RegistrationComponent implements OnInit { 16 title: any; 17 message: any; 18 myControl: FormGroup; 19 constructor(private fb: FormBuilder) {} 20 21 ngOnInit() { 22 this.title = 'Hello-app'; 23 this.message = 'FormControlを使う'; 24 this.myControl = this.fb.group({ 25 name: ['', [Validators.required]], 26 mail: ['', [Validators.email]], 27 age: [0, [Validators.min(1), Validators.max(150)]], 28 }); 29 } 30 31 get name() { 32 return this.myControl.get('name'); 33 } 34 get mail() { 35 return this.myControl.get('mail'); 36 } 37 get age() { 38 return this.myControl.get('age'); 39 } 40 41 onSubmit() { 42 if (this.myControl.invalid) { 43 this.message = 'VALIDATION ERROR.'; 44 } else { 45 let result = this.myControl.value; 46 this.message = JSON.stringify(result); 47 } 48 } 49} 50

html

1// registration.component.html 2<h1>新規会員登録画面</h1> 3<form [(formGroup)]="myControl" (ngSubmit)="onSubmit()"> 4 <table> 5 <tr> 6 <th>Name</th> 7 <td> 8 <div *ngIf="name.invalid">Nameは必須項目です.</div> 9 <div><input type="text" formControlName="name" /></div> 10 </td> 11 </tr> 12 <tr> 13 <th>Mail</th> 14 <td> 15 <div *ngIf="mail.invalid">メールアドレスが必要です.</div> 16 <div><input type="text" formControlName="mail" /></div> 17 </td> 18 </tr> 19 <tr> 20 <th>age</th> 21 <td> 22 <div *ngIf="age.invalid">1歳以上150歳以下で入力下さい..</div> 23 <input type="number" formControlName="age" /> 24 </td> 25 </tr> 26 <tr> 27 <th></th> 28 <td> 29 <input [disabled]="myControl.invalid" type="submit" value="click" /> 30 </td> 31 </tr> 32 </table> 33</form>

typescript

1// app.module.ts 2import { NgModule } from '@angular/core'; 3import { BrowserModule } from '@angular/platform-browser'; 4import { RouterModule, Routes } from '@angular/router'; 5import { 6 FormsModule, 7 ReactiveFormsModule, 8 FormGroup, 9 FormBuilder, 10 Validators, 11} from '@angular/forms'; 12 13import { AppRoutingModule } from './app-routing.module'; 14import { AppComponent } from './app.component'; 15import { TestComponent } from './test/test.component'; 16import { LoginComponent } from './login/login.component'; 17import { MypageComponent } from './mypage/mypage.component'; 18import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 19import { MatSliderModule } from '@angular/material/slider'; 20import { MatButtonModule } from '@angular/material/button'; 21import { MatCardModule } from '@angular/material/card'; 22import { MatInputModule } from '@angular/material/input'; 23import { MatFormFieldModule } from '@angular/material/form-field'; 24import { MatIconModule } from '@angular/material/icon'; 25 26const ROUTE_TABLE: Routes = [ 27 { path: 'test', component: TestComponent }, 28 { path: 'login', component: LoginComponent }, 29 { path: 'mypage', component: MypageComponent }, 30]; 31 32@NgModule({ 33 declarations: [AppComponent, TestComponent, LoginComponent, MypageComponent], 34 imports: [ 35 BrowserModule, 36 AppRoutingModule, 37 FormsModule, 38 ReactiveFormsModule, 39 RouterModule.forRoot(ROUTE_TABLE), 40 BrowserAnimationsModule, 41 MatSliderModule, 42 MatButtonModule, 43 MatCardModule, 44 MatInputModule, 45 MatFormFieldModule, 46 MatIconModule, 47 FormGroup, 48 FormBuilder, 49 Validators, 50 ], 51 providers: [], 52 bootstrap: [AppComponent], 53}) 54export class AppModule {} 55

typescript

1// registration-routing.module.ts 2import { NgModule } from '@angular/core'; 3import { RouterModule, Routes } from '@angular/router'; 4import { RegistrationComponent } from './registration.component'; 5 6const routes: Routes = [{ path: '', component: RegistrationComponent }]; 7 8@NgModule({ 9 imports: [RouterModule.forChild(routes)], 10 exports: [RouterModule], 11}) 12export class RegistrationRoutingModule {} 13

typescript

1// registration.module.ts 2import { NgModule } from '@angular/core'; 3import { CommonModule } from '@angular/common'; 4import { FormBuilder, FormGroup } from '@angular/forms'; 5 6import { RegistrationRoutingModule } from './registration-routing.module'; 7import { RegistrationComponent } from './registration.component'; 8 9@NgModule({ 10 declarations: [RegistrationComponent], 11 imports: [CommonModule, RegistrationRoutingModule, FormBuilder, FormGroup], 12}) 13export class RegistrationModule {} 14

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tshion

2021/08/22 04:01 編集

手元で試せていないのですが、ngOnInit() の内容をconstructor 内に移動して様子をみたいかもです。 変数宣言?した後に、その変数に対して値を設定していないのが原因かと疑っているので。
tonkotsu_ramen

2021/08/22 04:35

ご回答ありがとうございます。 試してみたところ、コンパイルは成功しましたが、ブラウザには何も表示されなくなりました。ブラウザには下記のようなエラーがでるようになりました。 core.js:4036 JIT compilation failed for NgModule class AppModule { } getCompilerFacade @ core.js:4036 get @ core.js:26920 getNgModuleDef @ core.js:1117 assertNgModuleType @ core.js:1296 compileNgModuleFactory__POST_R3__ @ core.js:29073 bootstrapModule @ core.js:29324 4431 @ main.ts:11 __webpack_require__ @ bootstrap:19 __webpack_exec__ @ (index):23 (anonymous) @ (index):23 __webpack_require__.O @ chunk loaded:23 (anonymous) @ (index):23 webpackJsonpCallback @ jsonp chunk loading:70 (anonymous) @ main.js:1 core.js:4055 Uncaught Error: The NgModule 'AppModule' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available. JIT compilation is discouraged for production use-cases! Consider using AOT mode instead. Alternatively, the JIT compiler should be loaded by bootstrapping using '@angular/platform-browser-dynamic' or '@angular/platform-server', or manually provide the compiler with 'import "@angular/compiler";' before bootstrapping. at getCompilerFacade (core.js:4055) at Function.get (core.js:26920) at getNgModuleDef (core.js:1117) at assertNgModuleType (core.js:1296) at compileNgModuleFactory__POST_R3__ (core.js:29073) at PlatformRef.bootstrapModule (core.js:29324) at Module.4431 (main.ts:11) at __webpack_require__ (bootstrap:19) at __webpack_exec__ ((index):23) at (index):23
tshion

2021/08/22 06:06

追記ありがとうございます。 おそらく本来の質問だった件は、ngOnInit() の内容をconstructor() に移動して、 ちゃんとメンバー変数を初期化したので、直ったのではないかと思いました。 で、そのあとに出たエラーメッセージですが、また別の問題な気がしております。 試しにng serve を一度止めて、改めてng serve し直して様子を見たいかもです。 それで特に変化がなかったら、もしかしたらmodule などの設定に不備があるかもなので、 そのあたりを確認したいかもです。
tonkotsu_ramen

2021/08/22 08:35

ありがとうございます。ng serveもし直してみましたが、症状は変わりませんでした。 関係ありそうなmodule関係のソースコードも追記致しました。私がみた限りではどこに原因があるのかわかっておりません。
tshion

2021/08/22 11:44

app.module.ts のルート定義を見ててふと思ったのですが、 こちら「新規会員登録画面」へのルート定義はありますでしょうか? それを足して様子を見たいかもです
guest

回答1

0

Property '○○○' has no initializer and is not definitely assigned in the constructor.

初期化されてない、って怒られているので、
ngOnInit() でやっている初期化を変数宣言時に行ってみるのはいかがでしょう?

export class AppComponent implements OnInit { title = 'Hello-app'; message = 'FormControlを使う'; myControl = this.fb.group({ name: ['', [Validators.required]], mail: ['', [Validators.email]], age: [0, [Validators.min(1), Validators.max(150)]], }); constructor(private fb: FormBuilder) {} ngOnInit() { }

のような感じで。

投稿2021/08/27 11:46

sassy

総合スコア45

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問