Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 类型';字符串';不可分配给类型';力矩';错误_Angular_Typescript_Momentjs - Fatal编程技术网

Angular 类型';字符串';不可分配给类型';力矩';错误

Angular 类型';字符串';不可分配给类型';力矩';错误,angular,typescript,momentjs,Angular,Typescript,Momentjs,我有一个这样的物体: import { Moment } from 'moment'; export interface INewsletter { id?: number; creationDate?: Moment; email?: string; } export class Newsletter implements INewsletter { constructor(public id?: number, public creationDate?:

我有一个这样的物体:

import { Moment } from 'moment';

export interface INewsletter {
    id?: number;
    creationDate?: Moment;
    email?: string;
}

export class Newsletter implements INewsletter {
    constructor(public id?: number, public creationDate?: Moment, public email?: string) {}
}
在一个地方,我需要从我正在使用的表单中获取日期,但在第二个给我带来问题的情况下,我只需要从系统中获取日期,并在新创建的对象中使用它(没有错误,我不理解,因为日期也是一个瞬间)


因此,在(1)和(2)两种情况下,我都无法使其工作,我不明白为什么。

在这两种情况下,您都尝试将字符串分配给
Newsletter.creationDate
,它需要一个
时刻
对象


案例(1)可以通过替换
moment().format(this.creationDate)来工作通过<代码>时刻(本创建日期)
由于
时刻
中的
.format()
方法返回一个字符串,正如

所述,在这两种情况下,您都尝试将一个字符串分配给
时事通讯.creationDate
,它需要一个
时刻
对象


案例(1)可以通过替换
moment().format(this.creationDate)来工作通过<代码>时刻(本创建日期)所提到的,
中的
.format()
方法返回字符串,因为
中的
format
函数返回字符串,而您的
this.newsletter.creationDate
是界面中的矩对象。所以你必须这样做
this.newsletter.creationDate=moment(this.creationDate)
.format()
仅用于格式化,因此您需要在此处传递一些格式,而不是CreationDate。对于这个明显的问题,谢谢,很抱歉。我不知道我在想什么,这是因为
format
函数from
moment
返回一个字符串,您的
this.newsletter.creationDate
是界面中的一个moment对象。所以你必须这样做
this.newsletter.creationDate=moment(this.creationDate)
.format()
仅用于格式化,因此您需要在此处传递一些格式,而不是CreationDate。对于这个明显的问题,谢谢,很抱歉。我不知道我在想什么
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import * as moment from 'moment';
import { DATE_TIME_FORMAT } from 'app/shared/constants/input.constants';

import { INewsletter } from 'app/shared/model/newsletter.model';
import { NewsletterService } from '../../entities/newsletter/newsletter.service';

@Component({
    selector: 'jhi-footer',
    templateUrl: './footer.component.html'
})
export class FooterComponent implements OnInit {
    newsletter: INewsletter;
    isSaving: boolean;
    creationDate: string;

    constructor(private newsletterService: NewsletterService, private activatedRoute: ActivatedRoute) {}

    ngOnInit() {
        this.isSaving = false;
        this.creationDate = moment().format(DATE_TIME_FORMAT);
        this.newsletter = new Object(); 
(1)     this.newsletter.creationDate = moment().format(this.creationDate); 
(2)     this.newsletter.creationDate = this.creationDate;
    }