Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add moment to angular 2 (angular cli)

I have followed the guid lines at the cli github page. But I am unable to make it work in my app.
Here is what I did:

  1. installed moment: npm install moment --save
  2. installed moment ts typings: npm install @types/moment --save
  3. imported moment in one of my components: import * as moment from 'moment';

I am getting the following error: img

Any ideas?

like image 691
vlio20 Avatar asked Sep 29 '16 08:09

vlio20


People also ask

What is Moment () in Angular?

Moment. js is used for parsing, validating, manipulating, and displaying dates and times in JavaScript. In this guide, we are adding Moment. js to Angular (application platform). It is worth noting that alternatives to using Moment.

Can we use moment js in Angular?

You can now use Moment. js in your Angular app, as long as you import it into the component in which you plan to use it.

How do I use moment in angular?

Basic Moment Usage. Run the following commands inside your Angular CLI project to install Moment and it’s corresponding interfaces. npm i -S moment; npm i -D @types/moment; Once this is done you can work with moment objects in your application like this: import * as moment from 'moment';

How to import moment in angular with typescript?

And then in your Angular app, import is as easy as this: import * as moment from 'moment'; That's it, you get full Typescript support! Bonus edit:To type a variable or property as Momentin Typescript you can do this e.g.: let myMoment: moment.Moment = moment("someDate");

Can I use angular2-moment in my App?

Using it in your app may not work the same way because this approach uses node's require, which can't be used client side. However, when using Moment in your components, you can use angular2-moment. Full setup instructions can be found on the GitHub page, but the usage looks like this:

How to import moment in angular2 with systemjs and JSPM?

for angular2 with systemjs and jspm had to do: import * as moment_ from 'moment'; export const moment = moment_["default"]; var a = moment.now(); var b = moment().format('dddd'); var c = moment().startOf('day').fromNow(); Share Follow answered Sep 7 '16 at 19:23


3 Answers

To install third party library in the newest version of angular-cli is just made simpler. (The webpack version, not systemjs one).

Go to your angular-cli.json on your project root and configure it like,

{
  ...
  "apps": [
     ...
     "scripts": [
        "../node_modules/moment/min/moment.min.js"
     ]
     ...
  ]
  ...
}

Then finally, in your any.component.ts you can import it like this,

import { Component, OnInit } from '@angular/core';
import * as moment from 'moment';

@Component({
    selector: '[id=home]',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
    constructor() { }

    ngOnInit () {
        console.log('today is', moment());
    }
}
like image 69
choz Avatar answered Oct 16 '22 11:10

choz


This thread is a little old but this is what I needed. Moment now comes with a type definition file and @types/moment is now deprecated in favor of this new file. In order to have moment work with other libraries which extend type definitions from moment like fullcallendar, you'll also need to override the default module resolution in your tsconfig.app.json

"compilerOptions": {
    ...
    "paths": {
        "moment": [
          "../node_modules/moment/moment.d.ts", 
          "../node_modules/moment/min/moment.min.js"
        ]
    },
},

You'll also want to update your angular.json build section to include moment:

 "build": {
      ...,
      "options": {
        ...
        "scripts": [ "node_modules/moment/min/moment.min.js"]
 },
like image 2
user1187839 Avatar answered Oct 16 '22 13:10

user1187839


You just need to include to include moment using the CommonJS syntax, rather than import. Try the following:

let moment = require('moment');

This is because moment is not as ES6 module yet, and as such won't work with the newer import syntax.

UPDATE:

When I think about it, I have only used this approach in my unit tests. Using it in your app may not work the same way because this approach uses node's require, which can't be used client side.

However, when using Moment in your components, you can use angular2-moment. Full setup instructions can be found on the GitHub page, but the usage looks like this:

<div>today is {{ Date.now() | amDateFormat:'LL' }}</div>

There are a few other pipes you can use, which are all documented on the GitHub page.

UPDATE 2:

As of v2.10.0, Moment now supports the ES6 syntax, so you should be able to use any ES6 import syntax instead of require.

like image 1
Maloric Avatar answered Oct 16 '22 11:10

Maloric