2017-04-16 100 views
1

我想導入一個外部js庫,瞬間進入離子2. 我有時刻模塊存儲在節點模塊中,但看起來網絡包不包括它在我的項目目錄中。Ionic2導入外部Js文件

<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> 

然而,當我看着我的瀏覽器,它不包括在該節點模塊

enter image description here

我已經安裝在我的nose_modules時刻然而,當離子建立它不會出現在我的瀏覽器一個文件

我正在使用Ionic 2和Angular 2.我的猜測是,我需要指定包括它如何通過模塊或網絡包沒有人知道如何做到這一點?

**編輯** 這裏是TS頁面類

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'); 
    } 
} 

回答

1

你只需要如下圖所示做。不需要做任何特別的事情。請參閱official doc here

npm install moment --save 

.TS

import moment from 'moment'; 

例如

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

當我的項目的基礎是無視我在node_modules路徑文件如圖片所示
+0

這些文件已經存在,但它們不是從節點提供的。 –

+0

只是刪除你的整個節點模塊文件夾中,之後做這個'故宮i' – Sampath

+0

沒有幫助 –