英文:
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
<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>
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 "@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 {}
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 "@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 {}
why I think it is nx related
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't bind to 'matDatepicker' since it isn't a known property of '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",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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't bind to 'htmlFor' since it isn't a known property of 'mat-datepicker-toggle'.
1. If 'mat-datepicker-toggle' is an Angular component and it has 'htmlFor' input, then verify that it is part of this module.
2. If 'mat-datepicker-toggle' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.
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",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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中按如下方式导入MatDatepickerModule
和MatNativeDateModule
:
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 '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material/core';
Add them in the imports array.
答案2
得分: 0
user-profile.component.ts
位于一个库中。也许它在该库的一个模块中声明。您还需要将 MatDatepickerModule
和 MatNativeDateModule
导入到该模块中。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论