Angular TypeScript和NGRX商店接口,访问正确的接口属性?

发布于 2025-02-13 07:19:28 字数 3454 浏览 3 评论 0原文

我只是看了一个教程,有点困惑: 接口注册用户:

export interface RegisterRequestInterface {
  user: {
    email: string,
    password: string,
    username: string,
  },
}

在操作中为ngrx商店而言。

export interface RegisterRequestInterface {
  user: {
    email: string,
    password: string,
    username: string,
  },
}

export const registerAction = createAction(
 ActionTypes.REGISTER,
  props<{ request: RegisterRequestInterface}>()
)

有一个供提交的 我的意思是有registerRequestInterface,但是该接口具有“ user”属性,因此不应该是request:regissionRequestInterface [“ user”]}?但是它似乎仍然有效,这是如何固有地工作的?它是否对“ user”等名称对象进行隐式类型的属性匹配? 另外,如果我在registerRequestface中添加其他属性,例如 {emailsubmittit:boolean},它并不抱怨没有提交表单的属性命名的字段……仍然有效,为什么?

编辑:添加的组件表格和代码

<div class="auth-page">
    <div class="container page">
        <div class="row">
            <div class="col-md-6 offset-md-3 col-xs-12">
                <h1 class="text-xs-center"> Sign up</h1>
                <p class="text-xs-center">
                    <a [routerLink]='["/login"]'>Have an account?</a>
                </p>
                <form [formGroup]="form" (ngSubmit)="onSubmit()">
                    <fieldset>
                        <fieldset class="form-group">
                            <input type="text" class="form-control form-control-lg " placeholder="Username " formControlName="username" />
                            <input type="email" class="form-control form-control-lg " placeholder="Email " formControlName="email" />
                            <input type="password" class="form-control form-control-lg " placeholder="Password " formControlName="password" />
                        </fieldset>
                        <button class="btn btn-lg btn-primary pull-xs-right " type="submit " [disabled]="isSubmitting$ | async">
                          Sign up 
                        </button>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div>

import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { select, Store } from "@ngrx/store";
import { Observable } from "rxjs";
import { registerAction } from "../../store/actions";
import { isSubmittingSelector } from "../../store/selectors";

@Component({
  selector: 'mc-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.components.scss']
})
export class RegisterComponent implements OnInit {
  form: FormGroup;
  isSubmitting$: Observable<boolean>

  constructor(private fb: FormBuilder, private store: Store) {

  }

  ngOnInit(): void {
    this.initializeForm();
    this.initializeValues();
  }

  initializeForm(): void {
    this.form = this.fb.group({
      username: ['', Validators.required],
      email:  ['', Validators.required, Validators.email],
      password:  ['', Validators.required],
    });
  }

  initializeValues():void {
    this.isSubmitting$ = this.store.pipe(select(isSubmittingSelector));
    console.log(this.isSubmitting$);
  }

  onSubmit(): void {
    console.log(this.form.value, this.form.valid);
    this.store.dispatch(registerAction(this.form.value));
  }

}

I just watched a tutorial and was a bit confused:
There was an interface for a submit for to register a user:

export interface RegisterRequestInterface {
  user: {
    email: string,
    password: string,
    username: string,
  },
}

and for Ngrx Store in the actions.ts:

export interface RegisterRequestInterface {
  user: {
    email: string,
    password: string,
    username: string,
  },
}

export const registerAction = createAction(
 ActionTypes.REGISTER,
  props<{ request: RegisterRequestInterface}>()
)

Here, I am just a little bit confused, how exactly is the action getting the correct props? I mean there is the RegisterRequestInterface, but that interface has a "user" property, so shouldn't it be {request: RegisterRequestInterface["user"]} ? But it seems to work nonetheless, how does this work inherently? Does it do an implicit type property match for a names object like "user"?
Also, if I add some other property inside RegisterRequestInterface like
{ emailSubmitted: boolean}, it doesn't complain that there is no field named like the property if submitting the form... and it still works, why?

EDIT: Added component form and code

<div class="auth-page">
    <div class="container page">
        <div class="row">
            <div class="col-md-6 offset-md-3 col-xs-12">
                <h1 class="text-xs-center"> Sign up</h1>
                <p class="text-xs-center">
                    <a [routerLink]='["/login"]'>Have an account?</a>
                </p>
                <form [formGroup]="form" (ngSubmit)="onSubmit()">
                    <fieldset>
                        <fieldset class="form-group">
                            <input type="text" class="form-control form-control-lg " placeholder="Username " formControlName="username" />
                            <input type="email" class="form-control form-control-lg " placeholder="Email " formControlName="email" />
                            <input type="password" class="form-control form-control-lg " placeholder="Password " formControlName="password" />
                        </fieldset>
                        <button class="btn btn-lg btn-primary pull-xs-right " type="submit " [disabled]="isSubmitting$ | async">
                          Sign up 
                        </button>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div>

import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { select, Store } from "@ngrx/store";
import { Observable } from "rxjs";
import { registerAction } from "../../store/actions";
import { isSubmittingSelector } from "../../store/selectors";

@Component({
  selector: 'mc-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.components.scss']
})
export class RegisterComponent implements OnInit {
  form: FormGroup;
  isSubmitting$: Observable<boolean>

  constructor(private fb: FormBuilder, private store: Store) {

  }

  ngOnInit(): void {
    this.initializeForm();
    this.initializeValues();
  }

  initializeForm(): void {
    this.form = this.fb.group({
      username: ['', Validators.required],
      email:  ['', Validators.required, Validators.email],
      password:  ['', Validators.required],
    });
  }

  initializeValues():void {
    this.isSubmitting$ = this.store.pipe(select(isSubmittingSelector));
    console.log(this.isSubmitting$);
  }

  onSubmit(): void {
    console.log(this.form.value, this.form.valid);
    this.store.dispatch(registerAction(this.form.value));
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文