Тип 'string' не может быть назначен для ошибки типа 'Moment' - PullRequest
0 голосов
/ 22 января 2019

У меня есть такой объект:

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) {}
}

В одном месте мне нужно получить дату из формы, которую я использую, но во втором случае, которая вызывает у меня проблему, мне просто нужно получить дату из системы и использовать ее в новом созданном объекте (без ошибки, что я не понимаю, так как дата тоже момент).

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;
    }

Итак, в обоих случаях (1) и (2) я не могу заставить его работать, и я не понимаю, почему.

1 Ответ

0 голосов
/ 22 января 2019

В обоих случаях вы пытаетесь назначить строку для Newsletter.creationDate, что предполагает Moment объект.

Case (1) может сработать, заменив moment().format(this.creationDate); на moment(this.creationDate); как *Метод 1007 * из moment возвращает строку, о которой упоминал @ Julien Metral

...