小编Uel*_*mer的帖子

打字稿对象转换不起作用

我有一个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输出一个普通的"对象".

有人可以给我一个建议吗?

types typescript angular

7
推荐指数
1
解决办法
5801
查看次数

自定义Angular 2指令不起作用

错误
模板解析错误:
无法绑定到'time-delta',因为它不是'p'的已知属性.

文档中的解决方案
我必须将指令添加到声明数组中.我已经这样做了.

现在我的架构:我有三个模块:

  • AppModule(root)
  • TimeModule(以后应该是一个辅助模块,带有一些指令)
  • AuthModule(登录,注册组件等)

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)

angular2-directives angular

4
推荐指数
1
解决办法
7769
查看次数

标签 统计

angular ×2

angular2-directives ×1

types ×1

typescript ×1