首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ionic2 / Angular 2平台导入

Ionic2 / Angular 2平台导入
EN

Stack Overflow用户
提问于 2016-07-14 06:09:00
回答 2查看 743关注 0票数 1

我正在尝试用ionic 2学习ag2,并尝试大多数简单的东西。

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

我跟踪了http://ionicframework.com/docs/v2/api/platform/Platform/

从‘ionic angular’导入{Platform};

代码语言:javascript
复制
@Component({...})
export MyPage {
  constructor(platform: Platform) {
    this.platform = platform;
  }
}

我的代码是-

代码语言:javascript
复制
    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]);
    });
  }

    }

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

代码语言:javascript
复制
[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上查看了与平台相关的typescript文件,它们都存在。快照-

EN

回答 2

Stack Overflow用户

发布于 2016-07-14 11:17:16

您需要在项目映射中定义变量platform。

代码语言:javascript
复制
export class ItemsMap {

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

}

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

代码语言:javascript
复制
export class ItemsMap {

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

     }

    }

您可以在Typescript类here上查看更多信息

票数 0
EN

Stack Overflow用户

发布于 2016-07-14 15:13:26

使用Typescript时,构造函数中的private/public单词是必需的:

代码语言:javascript
复制
constructor(private platform: Platform) {
      //...
 }

此外,您也不需要包含

代码语言:javascript
复制
this.platform = platform;

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

当我们“新建”类name

  • Initializes的实例时,
  • 声明构造函数参数及其类型
  • 声明同一个

的私有属性,该属性具有相应的参数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38362527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档