Angular Material日期选择器在Nx项目中无法正常工作。

huangapple go评论62阅读模式
英文:

Angular Material Datepicker does not work within Nx-Project

问题

以下是您要翻译的内容:

  • Stackblitz链接:Stackblitz

  • 我有一个使用Angular的nx项目,并且我在我的UI组件中使用Angular Material。我遇到了一个问题,无法在我的代码中实现mat-datepicker
    看起来像这样(来自这里的默认示例):

    <mat-form-field>
        <mat-label>Choose a date</mat-label>
        <input matInput [matDatepicker]="picker">
        <mat-hint>MM/DD/YYYY</mat-hint>
        <mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>
    
  • 由于我正在使用NX,我有一个名为ui的库,在其中有一个ui.models.ts文件。它看起来像这样:

    import { NgModule } from "@angular/core";
    import { CommonModule } from "@angular/common";
    import { RouterModule } from "@angular/router";
    import { uiRoutes } from "./lib.routes";
    import { HomeComponent } from "./home/home.component";
    import { BrowserModule } from "@angular/platform-browser";
    import { UserProfileComponent } from "./profile/user-profile/user-profile.component";
    import { DirectorProfileComponent } from "./profile/director-profile/director-profile.component";
    import { NavigationComponent } from "./navigation/navigation.component";
    import { LoginComponent } from "./auth/login/login.component";
    import { RegestrationComponent } from "./auth/regestration/regestration.component";
    import { MaterialModule } from "./material.module";
    
    @NgModule({
      imports: [CommonModule, RouterModule.forChild(uiRoutes), RouterModule, BrowserModule, MaterialModule],
      declarations: [HomeComponent, NavigationComponent, LoginComponent, RegestrationComponent, UserProfileComponent, DirectorProfileComponent],
      exports: [HomeComponent, NavigationComponent],
    })
    export class UiModule {}
    
  • MaterialModule是一个文件,我在其中导入了所有Angular Material模块。我创建了一个gist以便在这里有更多空间。MaterialModule 文件

  • 我在app.module.ts中导入了UiModule

    import { NgModule } from "@angular/core";
    import { BrowserModule } from "@angular/platform-browser";
    import { RouterModule } from "@angular/router";
    import { AppComponent } from "./app.component";
    import { appRoutes } from "./app.routes";
    import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
    import { UiModule } from "@protrack-2023/ui";
    
    @NgModule({
      declarations: [AppComponent],
      imports: [BrowserModule, RouterModule.forRoot(appRoutes, { initialNavigation: "enabledBlocking" }), BrowserAnimationsModule, UiModule],
      providers: [],
      bootstrap: [AppComponent],
    })
    export class AppModule {}
    

为什么我认为这与NX相关

我创建了一个新的不使用NX的Angular项目,它按预期工作...


错误消息:

错误:libs/ui/src/lib/profile/user-profile/user-profile.component.html:74:29 - 错误 NG8002:无法绑定到“matDatepicker”,因为它不是“input”的已知属性。

74             <input matInput [matDatepicker]="picker">
                               ~~~~~~~~~~~~~~~~~~~~~~~~

  libs/ui/src/lib/profile/user-profile/user-profile.component.ts:6:16
    6   templateUrl: "./user-profile.component.html",
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    该错误发生在UserProfileComponent组件的模板中。


错误:libs/ui/src/lib/profile/user-profile/user-profile.component.html:76:50 - 错误 NG8002:无法绑定到“htmlFor”,因为它不是“mat-datepicker-toggle”的已知属性。
1. 如果“mat-datepicker-toggle”是Angular组件,并且它有“htmlFor”输入,请验证它是否属于此模块。
2. 如果“mat-datepicker-toggle”是Web组件,请将“CUSTOM_ELEMENTS_SCHEMA”添加到此组件的“@NgModule.schemas”以抑制此消息。
3. 要允许任何属性,请将“NO_ERRORS_SCHEMA”添加到此组件的“@NgModule.schemas”。

76             <mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
                                                    ~~~~~~~~~~~~~~

  libs/ui/src/lib/profile/user-profile/user-profile.component.ts:6:16
    6   templateUrl: "./user-profile.component.html",
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    该错误发生在UserProfileComponent组件的模板中。

是否有人经历过与我类似的问题?

我创建了一个新项目,没有使用NX,它正常工作。

英文:

Stackblitz right here: Stackblitz

I have a nx project with angular and I use angular material for my ui components. I have this issue where I cannot implement a mat-datepicker in my code.
Looks like this: (default example from here

&lt;mat-form-field&gt;
    &lt;mat-label&gt;Choose a date&lt;/mat-label&gt;
    &lt;input matInput [matDatepicker]=&quot;picker&quot;&gt;
    &lt;mat-hint&gt;MM/DD/YYYY&lt;/mat-hint&gt;
    &lt;mat-datepicker-toggle matIconSuffix [for]=&quot;picker&quot;&gt;&lt;/mat-datepicker-toggle&gt;
    &lt;mat-datepicker #picker&gt;&lt;/mat-datepicker&gt;
&lt;/mat-form-field&gt;

As I'm working with NX I have a library called ui in which I have a ui.models.ts file.
It looks like this:

import { NgModule } from &quot;@angular/core&quot;;
import { CommonModule } from &quot;@angular/common&quot;;
import { RouterModule } from &quot;@angular/router&quot;;
import { uiRoutes } from &quot;./lib.routes&quot;;
import { HomeComponent } from &quot;./home/home.component&quot;;
import { BrowserModule } from &quot;@angular/platform-browser&quot;;
import { UserProfileComponent } from &quot;./profile/user-profile/user-profile.component&quot;;
import { DirectorProfileComponent } from &quot;./profile/director-profile/director-profile.component&quot;;
import { NavigationComponent } from &quot;./navigation/navigation.component&quot;;
import { LoginComponent } from &quot;./auth/login/login.component&quot;;
import { RegestrationComponent } from &quot;./auth/regestration/regestration.component&quot;;
import { MaterialModule } from &quot;./material.module&quot;;

@NgModule({
  imports: [CommonModule, RouterModule.forChild(uiRoutes), RouterModule, BrowserModule, MaterialModule],
  declarations: [HomeComponent, NavigationComponent, LoginComponent, RegestrationComponent, UserProfileComponent, DirectorProfileComponent],
  exports: [HomeComponent, NavigationComponent],
})
export class UiModule {}

The MaterialModule is a file where I imported all angular material modules. I created a gist so there is more space here. MaterialModule File

I import the UiModule at app.module.ts.

import { NgModule } from &quot;@angular/core&quot;;
import { BrowserModule } from &quot;@angular/platform-browser&quot;;
import { RouterModule } from &quot;@angular/router&quot;;
import { AppComponent } from &quot;./app.component&quot;;
import { appRoutes } from &quot;./app.routes&quot;;
import { BrowserAnimationsModule } from &quot;@angular/platform-browser/animations&quot;;
import { UiModule } from &quot;@protrack-2023/ui&quot;;

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, RouterModule.forRoot(appRoutes, { initialNavigation: &quot;enabledBlocking&quot; }), BrowserAnimationsModule, UiModule],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

I created a new angular project without nx and it work as expected...


Error Message:

Error: libs/ui/src/lib/profile/user-profile/user-profile.component.html:74:29 - error NG8002: Can&#39;t bind to &#39;matDatepicker&#39; since it isn&#39;t a known property of &#39;input&#39;.
74             &lt;input matInput [matDatepicker]=&quot;picker&quot;&gt;
~~~~~~~~~~~~~~~~~~~~~~~~
libs/ui/src/lib/profile/user-profile/user-profile.component.ts:6:16
6   templateUrl: &quot;./user-profile.component.html&quot;,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component UserProfileComponent.
Error: libs/ui/src/lib/profile/user-profile/user-profile.component.html:76:50 - error NG8002: Can&#39;t bind to &#39;htmlFor&#39; since it isn&#39;t a known property of &#39;mat-datepicker-toggle&#39;.
1. If &#39;mat-datepicker-toggle&#39; is an Angular component and it has &#39;htmlFor&#39; input, then verify that it is part of this module.
2. If &#39;mat-datepicker-toggle&#39; is a Web Component then add &#39;CUSTOM_ELEMENTS_SCHEMA&#39; to the &#39;@NgModule.schemas&#39; of this component to suppress this message.
3. To allow any property add &#39;NO_ERRORS_SCHEMA&#39; to the &#39;@NgModule.schemas&#39; of this component.
76             &lt;mat-datepicker-toggle matIconSuffix [for]=&quot;picker&quot;&gt;&lt;/mat-datepicker-toggle&gt;
~~~~~~~~~~~~~~
libs/ui/src/lib/profile/user-profile/user-profile.component.ts:6:16
6   templateUrl: &quot;./user-profile.component.html&quot;,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component UserProfileComponent.

Had someone the same experiance as me?

I created a new project without nx and it worked fine.

答案1

得分: 0

请在app.module.ts中按如下方式导入MatDatepickerModuleMatNativeDateModule

import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material/core';

将它们添加到imports数组中。

英文:

Please import MatDatepickerModule and MatNativeDateModule in the app.module.ts as below:

import { MatDatepickerModule } from &#39;@angular/material/datepicker&#39;;
import { MatNativeDateModule } from &#39;@angular/material/core&#39;;

Add them in the imports array.

答案2

得分: 0

user-profile.component.ts 位于一个库中。也许它在该库的一个模块中声明。您还需要将 MatDatepickerModuleMatNativeDateModule 导入到该模块中。

英文:

I see that user-profile.component.ts lives in a library. Perhaps it is declared in a module in that library. You will need to import MatDatepickerModule and MatNativeDateModule to that module as well.

答案3

得分: 0

我升级到最新的nx工作区版本(16.3.2),它也将Angular和Angular Material更新到了v16。这解决了我的问题。

英文:

SOLUTION

I updated to the lastes nx workspace verion (16.3.2) which also updated angular and angular material to v16. It solved the problem for me.

huangapple
  • 本文由 发表于 2023年5月29日 00:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定