我有一个HTTP API.我正在加载一组对象作为json.
我想要转换为Typescript对象,但是使用关键字"as"它不起作用,甚至不能在对象前面使用<Type>.
r.forEach(entry => {
entry.creationDate = new Date(entry.creationDate.date);
entry.creator = <User>entry.creator;
return entry;
});
Run Code Online (Sandbox Code Playgroud)
在entry.creator转换后直接输出console.log输出一个普通的"对象".
有人可以给我一个建议吗?
错误
模板解析错误:
无法绑定到'time-delta',因为它不是'p'的已知属性.
文档中的解决方案
我必须将指令添加到声明数组中.我已经这样做了.
现在我的架构:我有三个模块:
AppModule:
@NgModule({
imports: [
TimeModule,
BrowserModule,
FormsModule,
AuthModule,
routing,
],
declarations: [
AppComponent
],
providers: [
appRoutingProviders
],
bootstrap: [AppComponent]
})
Run Code Online (Sandbox Code Playgroud)
AuthModule:
@NgModule({
imports: [
BrowserModule,
FormsModule,
authRouting
],
declarations: [
AuthComponent,
LoginComponent,
SignupComponent,
LogoutComponent
],
providers: [
AuthGuard,
AuthService
],
bootstrap: [ LoginComponent ]
})
Run Code Online (Sandbox Code Playgroud)
TimeModule:
@NgModule({
declarations: [
ReadableDatePipe,
TimeDeltaDirective
]
})
Run Code Online (Sandbox Code Playgroud)
现在我试图在LoginComponent的视图中使用我的TimeDeltaDirective.我已经尝试将指令添加到其他声明数组中,但这也不起作用.
我感谢任何支持!谢谢
TimeDeltaDirective:
import { Directive, ElementRef, Input, Renderer } from '@angular/core';
@Directive({ selector: '[time-delta]' })
export …Run Code Online (Sandbox Code Playgroud)