【问题标题】:error TS1005: ':' expected TypeScript Angular6错误 TS1005:“:”预期 TypeScript Angular6
【发布时间】:2018-08-20 19:19:22
【问题描述】:

我在尝试构建 Angular 6 应用程序时遇到错误。

src/app/util/notification.service.ts(14,9) 中的错误:错误 TS1005: ':' 预期。

这里是相关代码

import { Injectable } from '@angular/core';
import { ToastrService } from 'ngx-toastr';

@Injectable()
export class NotificationService {

    timeOut: number = 5000;

    constructor(private toastr: ToastrService) {}

    error(toast_msg, msg_title){

            this.toastr.error('<span class="now-ui-icons ui-1_bell-53"></span> ' + toast_msg, msg_title, {
                this.timeOut
            });
   }

}

可能是什么问题?

【问题讨论】:

  • @JoelHarkes super() 为扩展其他类的类调用。至于 OP {this.timeOut} 将不起作用,除非 两个变量具有完全相同的名称。并且 timeoutthis.timeOut 不同:考虑明确声明它。

标签: angular typescript tslint


【解决方案1】:

你可能想要这样的东西:

this.toastr.error('<span class="now-ui-icons ui-1_bell-53"></span> ' + toast_msg, msg_title, {
  timeout: this.timeOut,
});

或者,因为其余参数作为 args 传递:

this.toastr.error('<span class="now-ui-icons ui-1_bell-53"></span> ' + toast_msg, msg_title, this.timeOut);

【讨论】:

  • 感谢您的回答。我的错。应该是timeout: this.timeOut
【解决方案2】:

错误与 TypeScript 配置有关。

通过显式指定属性名称来创建您的对象

{ timeout: this.timeOut }

【讨论】:

  • 感谢您的回答。是的,我的代码中缺少属性名称。这就是问题所在。
【解决方案3】:

你的问题是没有使用键值对进行超时

试试这个,

error(toast_msg, msg_title) {
        this.toastr.error('<span class="now-ui-icons ui-1_bell-53"></span> ' + toast_msg, msg_title, {
            timeOut: this.timeOut
        });
    }

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 2017-11-24
    • 1970-01-01
    • 2023-03-25
    • 2018-03-06
    • 2013-08-20
    • 2017-07-08
    • 1970-01-01
    • 2019-06-23
    相关资源
    最近更新 更多