简体   繁体   English

将 typescript 文件导入 angular typescript 文件

[英]Import typescript file to an angular typescript file

I have a type script file like this:我有一个像这样的类型脚本文件:

 class Student { fullName: string; constructor(public firstName: string, public middleInitial: string, public lastName: string) { this.fullName = firstName + " " + middleInitial + " " + lastName; } } interface Person { firstName: string; lastName: string; } function greeter(person: Person) { return "Hello, " + person.firstName + " " + person.lastName; } let user = new Student("Jane", "M.", "User"); document.body.textContent = greeter(user);

I want to add it into an angular component (exactly in it typescript file).我想将它添加到 angular 组件中(正好在 typescript 文件中)。 How can I do that?我怎样才能做到这一点? (I have searched but I haven't found any answer I need) (我已经搜索过,但我没有找到任何我需要的答案)

You can do this你可以这样做

create.ts file name model.ts create.ts 文件名model.ts

inside that在里面

export interface  Person {
    firstName: string;
    lastName: string;
}

in accessing page eg: home.ts在访问页面例如: home.ts

import file导入文件

import * as models_app from '../../models';

set value设定值

 private selectedInstrument: models_app.Person = {
        firstName:"chanaka";
        lastName:"weerasinghe";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM