从Angular 2中

时间:2018-01-30 19:08:30

标签: angular date npm

我需要从当前时间减去天/小时/分钟。要做到这一点并关注npmjs网站,我已完成此安装:npm install --save add-subtract-date。 然后在TS文件中,我有这样的代码:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

const addSubtractDate = require('add-subtract-date');
let d = new Date(); //line 12
console.log(this.addSubtractDate.subtract(d, 25, 'days'));
console.log(this.addSubtractDate.add(d, 59, 'minutes'));
} //line 15
然而,我得到了这些编译时错误:

ERROR in src/app/app.component.ts(12,1): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
src/app/app.component.ts(15,1): error TS1128: Declaration or statement expected.

控制台中的这些错误:

Uncaught TypeError: Cannot read property 'subtract' of undefined
    at eval (app.component.ts:13)
    at Object.../../../../../src/app/app.component.ts (main.bundle.js:28)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (app.module.ts:5)
    at Object.../../../../../src/app/app.module.ts (main.bundle.js:36)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (main.ts:4)
    at Object.../../../../../src/main.ts (main.bundle.js:52)
    at __webpack_require__ (inline.bundle.js:55)
    at Object.0 (main.bundle.js:67)

你能否告诉我的代码有什么问题?

如果您有关于如何访问符合我目的的功能的任何(其他)建议?

1 个答案:

答案 0 :(得分:1)

您只需要导入

class A
{
    //this will be called on an instance of B or C, A will never be 
    //instantiated
    void foo()
    {
        ba<T>();
    }
}


class B :public A
{
//T want to save here the Type of T so i won't have to call foo() with 
//a template
}

class C :public A
{
//here comes another Type for T
}

然后,

<main>
 <ul>
     <li><span>1<br>£200</span></li>
</ul>
</main>

我个人不建议这样做,你总是可以使用原生JS来计算日期。