【问题标题】:Momentjs, Property 'toISOString' does not exist on type '{}'Momentjs,类型“{}”上不存在属性“toISOString”
【发布时间】:2019-07-17 14:07:46
【问题描述】:

我有以下代码直到现在都可以使用,但现在value.toISOString() 会引发编译器错误。我已经从 Angular 7 -> 8 升级到了 Typescript 3.4.5。 知道是什么原因造成的吗?

    import * as moment from 'moment';    
    ...

    private getQueryStringParameters(parameters: any) {
        if (!parameters) {
            return '';
        }

        let queryString = '?';

        // tslint:disable-next-line:forin
        for (const key in parameters) {
            const value = parameters[key];

            if (value !== undefined) {
                if (value instanceof Array) {
                    value.forEach(
                        item =>
                            (queryString +=
                                key + '=' + encodeURIComponent('' + item) + '&')
                    );
                } else if (value instanceof moment) {
                    queryString +=
                        key +
                        '=' +
                        encodeURIComponent('' + value.toISOString()) +
                        '&';
                } else {
                    queryString +=
                        key + '=' + encodeURIComponent('' + value) + '&';
                }
            }
        }

提前感谢您的任何意见!

【问题讨论】:

  • 出于好奇,你为什么要首先编写一个getQueryStringParameters 函数,而不是依赖 Angular 自己的从对象中形成查询参数字符串的方法(对于 Angular Router 和 HttpClient )?
  • 这个输出是什么typeof value
  • @robert 谢谢,将其更改为修复了编译问题。如果您将其添加为答案,我会接受。
  • @mbojko 我不确定,我没有写这段代码。但我会调查一下。

标签: angular typescript momentjs


【解决方案1】:

代替:

value instanceof moment

尝试使用 isMoment 方法:

moment.isMoment(value)

查看docsstackblitz 演示。

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 2021-07-10
    • 2017-07-22
    • 2023-03-26
    • 2023-04-02
    • 2019-12-20
    • 2021-12-07
    • 2016-03-14
    相关资源
    最近更新 更多