如何在Ionic2 / typescript中创建和使用自定义类 [英] How to create and use custom classes in Ionic2/typescript

查看:95
本文介绍了如何在Ionic2 / typescript中创建和使用自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OOP和打字稿的新手。我正在研究Ionic2项目。

I am new to OOP and typescript. I am working on Ionic2 project.

我有一个页面列表

list
- list.html
- list.ts
- list.scss

我在同一个文件夹中创建了一个新的类/ .ts文件

I have create a new class/.ts file in same folder

export class TestClass{
    constructor(){
        console.log("I am test class");
    }
}

然后在list.ts中我使用

then In list.ts I use

import { Component,ViewChild,ElementRef,Renderer2 } from '@angular/core';
import { NavController, NavParams,Platform,Content } from 'ionic-angular';

import {TestClass} from 'test.ts';

declare var Phaser:any;

@Component({
  selector: 'page-list',
  templateUrl: 'list.html'
})
export class ListPage {

  testClass:any = new TestClass();

  constructor(public navCtrl: NavController, public navParams: NavParams, private _platform:Platform) {


  };
};//end class

这给我发生错误不能查找模块test.ts

如何在Ionic2页面中导入我的自定义类?

How can i import my custom class in Ionic2 page?

请帮忙。

推荐答案

首先删除后面的勾号`。
其次在名为test.ts的同一文件夹中创建一个文件。
第三次粘贴代码导出类TestClass ...
最后从'./test'导入{TestClass}; 使用 ./

First remove the back ticks `. Second create a file in the same folder named test.ts. Third paste the code export class TestClass... Finally import {TestClass} from './test'; using ./

这篇关于如何在Ionic2 / typescript中创建和使用自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆