Angularjs Ionic2/Angular 2平台导入
我正在尝试用离子2学习ag2,并尝试最简单的事情 我正在尝试导入平台,并在我的ionic 2应用程序中获取必要的详细信息 我跟着 从“离子角度”导入{Platform}Angularjs Ionic2/Angular 2平台导入,angularjs,cordova,angular,ionic2,Angularjs,Cordova,Angular,Ionic2,我正在尝试用离子2学习ag2,并尝试最简单的事情 我正在尝试导入平台,并在我的ionic 2应用程序中获取必要的详细信息 我跟着 从“离子角度”导入{Platform} @Component({...}) export MyPage { constructor(platform: Platform) { this.platform = platform; } } 我的密码- import {Component} from '@angular/core'; imp
@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
我有一种感觉,我在这里错过了一些基本的东西,我不确定
我还检查了与平台相关的typescript文件的node_模块,它们都存在。快照-
您需要在项目映射中定义变量平台
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类的更多信息您需要在项目映射中定义变量平台
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类的更多信息在使用Typescript时,构造函数中必须使用私有/公共单词:
constructor(private platform: Platform) {
//...
}
此外,您不需要包括
this.platform = platform;
因为只要在构造函数中添加private-platform:platform
,这个简短的语法就可以起到很多作用:
- 声明构造函数参数及其类型
- 声明同名的私有属性
- 使用 当我们“新建”类的实例时对应的参数
constructor(private platform: Platform) {
//...
}
此外,您不需要包括
this.platform = platform;
因为只要在构造函数中添加private-platform:platform
,这个简短的语法就可以起到很多作用:
- 声明构造函数参数及其类型
- 声明同名的私有属性
- 使用 当我们“新建”类的实例时对应的参数
平台
是Ionic 2框架的一部分,但是导航器
属性是的一部分,因此答案会有所不同。谢谢您提供的详细信息。我理解这一点,但我关心的是大多数变量,如“navigator”,它在构建过程中似乎不起作用,对于this.networkState也是如此。我感到困惑的是,它是否需要围绕导入或本地变量进行定义,以及如何识别。谢谢你的帮助,真是与众不同。我建议您创建一个新问题,因为平台
是Ionic 2框架的一部分,但是导航器
属性是框架的一部分,因此答案会有所不同。