我正在尝试用ionic 2学习ag2,并尝试大多数简单的东西。
我正在尝试导入平台,并在我的ionic 2应用程序中获取必要的详细信息。
我跟踪了http://ionicframework.com/docs/v2/api/platform/Platform/
从‘ionic angular’导入{Platform};
@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]);
});
}
}NOw我正在尝试通过gulp构建并获得以下输出来构建代码库-
[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文件,它们都存在。快照-

发布于 2016-07-14 11:17:16
您需要在项目映射中定义变量platform。
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上查看更多信息
发布于 2016-07-14 15:13:26
使用Typescript时,构造函数中的private/public单词是必需的:
constructor(private platform: Platform) {
//...
}此外,您也不需要包含
this.platform = platform;因为只要将private platform : Platform添加到您的构造函数中,这个简短的语法就可以做很多事情:
当我们“新建”类name
的私有属性,该属性具有相应的参数
https://stackoverflow.com/questions/38362527
复制相似问题