Ionic2 / Angular 2平台导入

时间:2016-07-13 22:09:00

标签: angularjs cordova angular ionic2

我正在尝试用离子2学习ag2并尝试最简单的事情。

我正在尝试导入平台,并在我的离子2应用程序中获取必要的详细信息。

我关注http://ionicframework.com/docs/v2/api/platform/Platform/

从&ion; angular-angular';

导入{平台}
@Component({...})
export MyPage {
  constructor(platform: Platform) {
    this.platform = platform;
  }
}

我的代码 -

    import {Component} from '@angular/core';
    import {Platform} from 'ionic-angular';

    @Component({
      templateUrl: 'build/pages/items-map/items-map.html'
    })
    export class ItemsMap {

      constructor(platform: Platform) {
          this.platform = platform;
     }

btainNetworkConnection() {
    this.platform.ready().then(() => {
        this.networkState = navigator.connection.type;

        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.CELL]     = 'Cell generic connection';
        states[Connection.NONE]     = 'No network connection';

        alert('Connection type: ' + states[this.networkState]);
    });
  }

    }

我是否正在尝试通过gulp build构建代码库并获得以下输出 -

[00:06:26] Using gulpfile ~/Documents/Projects/Ionic2_1/MyIonic2Project/gulpfile.js
[00:06:26] Starting 'clean'...
[00:06:26] Finished 'clean' after 14 ms
[00:06:26] Starting 'build'...
[00:06:26] Starting 'sass'...
[00:06:26] Starting 'html'...
[00:06:26] Starting 'fonts'...
[00:06:26] Starting 'scripts'...
[00:06:26] Finished 'html' after 45 ms
[00:06:26] Finished 'scripts' after 43 ms
[00:06:26] Finished 'fonts' after 48 ms
[00:06:26] Finished 'sass' after 637 ms
TypeScript error: /Users/kray/Documents/Projects/Ionic2_1/MyIonic2Project/app/pages/item-map/items-map.ts(10,12): Error TS2339: Property 'platform' does not exist on type 'ItemsMap'.
[00:06:28] Finished 'build' after 2.28 s

我感觉我错过了一些基本的东西,我不确定。

我还检查了node_modules是否存在与平台相关的打字稿文件。快照 -

enter image description here

2 个答案:

答案 0 :(得分:0)

您需要在项目地图中定义变量平台。

export class ItemsMap {

platform:Platform//my addition
  constructor(platform: Platform) {
      this.platform = platform;
 }

}

或者在构建构造函数快捷方式中使用typescript来声明属性,如

export class ItemsMap {

    //notice modifier on constructor 
      constructor(public platform: Platform){

     }

    }

您可以在Typescript类here

上查看更多信息

答案 1 :(得分:0)

使用Typescript时,私有/公共字在构造函数中是必需的:

constructor(private platform: Platform) {
      //...
 }

此外,您不需要包含

this.platform = platform;

因为只需在构造函数中添加private platform : Platform,这个简短的语法可以做很多事情:

  • 声明构造函数参数及其类型
  • 声明一个同名的私有财产
  • 使用。初始化该属性 当我们" new"类的实例