Angular2 将 PrimeNG 添加到我的解决方案标签未定义
我已经使用 angular 三天了,终于用 web api 解决方案启动并运行了一个登录页面仪表板,并且运行良好,所以我的以下问题很可能是我的愚蠢,请耐心等待。
我无法加载 PrimeNG DataTableModule 来处理我的组件,我搜索了有关我的错误的类似帖子,我收集到的信息是以下错误可能有两个原因:无法绑定到“value”,因为它不是“p-dataTable”的已知属性。
可能有两个原因: 1) 该模块未在 app.module.ts 中定义。 2) 或属性不存在。
我的第一个问题 :为什么我的解决方案将我的 app.module.ts 拆分成三个 ts 文件 [app.module.client.ts、app.module.server.ts 和 app.module.shared.ts]?
我的第二个问题 当将 DataTableModule 添加到 app.module.client.ts 时,我收到此错误:无法绑定到“value”,因为它不是“p-dataTable”的已知属性。 将 DataTableModule 添加到共享或服务器模块文件时,我收到:由于错误导致预渲染失败:ReferenceError:未定义事件 我做错了什么?
这是我的代码: app.module.client.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { sharedConfig } from './app.module.shared';
import { AuthManager } from './auth/auth.manager';
import { UserIdentity } from './auth/user.identity';
import { UserService } from './services/user.service';
import { DataTableModule, SharedModule } from 'primeng/primeng';
@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
DataTableModule,
SharedModule,
BrowserModule,
FormsModule,
...sharedConfig.imports,
],
providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin },
AuthManager,
UserIdentity,
UserService
],
})
export class AppModule {
}
app.module.server.ts
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { FormsModule } from '@angular/forms';
import { sharedConfig } from './app.module.shared';
import { AuthManager } from './auth/auth.manager';
import { UserIdentity } from './auth/user.identity';
import { UserService } from './services/user.service';
@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
ServerModule,
FormsModule,
...sharedConfig.imports
],
providers: [ AuthManager,
UserIdentity,
UserService]
})
export class AppModule {
}
app.module.shared.ts
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { DashboardComponent } from './components/dashboad/dashboad.component';
import { AppComponent } from './components/app/app.component';
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';
import { LoginComponent } from './components/login/login.component';
import { AdminComponent } from './components/admin/admin.component';
import { SettingComponent } from './components/admin/settings/setting.component';
import { UserComponent } from './components/admin/users/user.component';
import { AuthManager } from './auth/auth.manager';
export const sharedConfig: NgModule = {
bootstrap: [AppComponent],
declarations: [
DashboardComponent,
AdminComponent,
UserComponent,
SettingComponent,
AppComponent,
NavMenuComponent,
CounterComponent,
FetchDataComponent,
LoginComponent,
HomeComponent,
],
imports: [
FormsModule,
RouterModule.forRoot([
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{
path: 'dashboard', component: DashboardComponent, children: [
{ path: 'home', component: HomeComponent, canActivate: [AuthManager] },
{ path: 'admin', component: AdminComponent, canActivate: [AuthManager], children: [
{ path: 'users', component: UserComponent, canActivate: [AuthManager] },
{ path: 'settings', component: SettingComponent, canActivate: [AuthManager] },
]},
{ path: 'fetch-data', component: FetchDataComponent, canActivate: [AuthManager] },
]
},
{ path: '**', redirectTo: 'home' }
]),
]
};
我尝试实现数据网格的页面 user.component.html
<p-dataTable [value]="users">
<p-column field="Email" header="Email"></p-column>
<p-column field="Name" header="Name"></p-column>
<p-column field="Surname" header="Surname"></p-column>
<p-column field="Cellphone" header="Cellphone"></p-column>
<p-column field="Telephone" header="Telephone"></p-column>
</p-dataTable>
user.component.ts
import { Component, OnInit } from '@angular/core';
import { UserService, UserViewModel } from '../../../services/user.service';
import 'rxjs/add/operator/toPromise';
import { Observable } from 'rxjs/Observable';
import { DataTableModule, SharedModule } from 'primeng/primeng';
@Component({
selector: 'user',
templateUrl: './user.component.html'
})
export class UserComponent implements OnInit {
users: UserViewModel[];
constructor(private userService: UserService) {
}
ngOnInit() {
this.userService.getUsers().subscribe(users => {
this.users = users;
});
}
}
在 app.module.client.ts 中定义的模块出现错误消息
Exception: Call to Node module failed with error: Error: Template parse errors: Can't bind to 'value' since it isn't a known property of 'p-dataTable'. 1. If 'p-dataTable' is an Angular component and it has 'value' input, then verify that it is part of this module. 2. If 'p-dataTable' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
在app.module.server.ts 或 app.module.shared.ts
Exception: Call to Node module failed with error: Prerendering failed because of error: ReferenceError: Event is not defined main-server.js:50676:38)
您是否尝试过将datatablemodule和sharedModule导入到sharedConfig ngmodule?
我对三个split app.module文件不熟悉,所以我无法对此发表评论。
< < < P>这是旁边的,但是正如Primeng所说,如果您使用这样的导入语句:687070320
,那么它会导入所有组件,这可能不是您的内容想。在这里查看他们的评论: https://wwwww.primefaces.org/primeng/primeng/primeng/#/setup/setup >
因此,问题在于服务器端预渲染未找到工具集的依赖项,因此解决方案是将 app.module.shared.ts 中的导入模块部分从仅使用:
import { DataTableModule, SharedModule } from 'primeng/primeng';
更改为
import { DataTableModule } from 'primeng/components/datatable/datatable';
import { BlockUIModule } from 'primeng/components/blockui/blockui';
因此,现在工具集在服务器端也具有完整路径,并且我仍然可以在服务器上使用预渲染。
感谢这篇文章: asp.net core、angular 2、PrimeNG