johnny 5
johnny 5

Reputation: 21033

Ionic2 Import external Js File

I'm trying to import an external js library, moment into Ionic 2. I have the moment modules stored in node modules however it seems web pack is not including it in my project directory.

<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>

  <script src="node_modules/moment/moment.js"></script>

</body>

However when I look in my browser it is not included in the node modules

enter image description here

I have moment installed in my nose_modules however when ionic builds it does not appear in my browser as a file

I'm using Ionic 2 and Angular 2. My guess is that I need to specify to include it some how through the modules or web pack does anyone know how to do this?

** Edit ** Here is the ts page class

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { User } from '../../models/user';
import { Users } from '../../providers/users';
import moment from 'moment';


@Component({
  selector: 'page-profile',
  templateUrl: 'profile.html'
})
export class ProfilePage {

    userId: number;
    user: User;

    getAge = () => {
        if (this.user) {
            return moment().diff(this.user.birthday, 'years');
        }
    }

    constructor(public navCtrl: NavController, public navParams: NavParams, private usersProvider: Users) {

        this.userId = navParams.get('userId');

        usersProvider.get(this.userId).subscribe(user => {
            this.user = user;
            console.log(user)
        });
    }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ProfilePage');
  }
}

Upvotes: 1

Views: 1820

Answers (1)

Sampath
Sampath

Reputation: 65988

You just need to do as shown below. No need to do any special things.

Please see the official doc here.

npm install moment --save

.ts

import moment from 'moment';

e.g.

let month = moment(date).format('MMMM');

Upvotes: 1

Related Questions