最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript - Calling a class exported from another file. - Stack Overflow

programmeradmin1浏览0评论

I have a file called searchBar.ts in which contains a init() function that I wish to call in a file called index.ts.

My searchBar.ts file looks like this:

class searchBar {
    init() {
        console.log('work you C*&*T')
    }
}

export let searchBarClass = new searchBar;

I then want to be able to call this function in index.ts. I am currently trying to do this with the below code but init is never found with my intellisense:

import { searchBarClass } from './modules/searchBar';

class Main {
    searchBarClass.init()
}

let main = new Main();
export {main}

Later on I then want to wrap the function in a global function that I'll call in the HTML file.

Let me know your thoughts

I have a file called searchBar.ts in which contains a init() function that I wish to call in a file called index.ts.

My searchBar.ts file looks like this:

class searchBar {
    init() {
        console.log('work you C*&*T')
    }
}

export let searchBarClass = new searchBar;

I then want to be able to call this function in index.ts. I am currently trying to do this with the below code but init is never found with my intellisense:

import { searchBarClass } from './modules/searchBar';

class Main {
    searchBarClass.init()
}

let main = new Main();
export {main}

Later on I then want to wrap the function in a global function that I'll call in the HTML file.

Let me know your thoughts

Share Improve this question asked Apr 1, 2017 at 13:55 MaxwellLynnMaxwellLynn 9786 gold badges26 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

If you want to export a class and call the init() method on the object instance:

export class SearchBar {
    init() {
        console.log('work you C*&*T')
    }
}

Then accessing it from another file:

import { SearchBar } from './modules/searchBar';

export class Main {
    constructor() {
        let searchBar = new SearchBar();
        searchBar.init();
    }
}

let main = new Main();

If you want to access the init() function statically:

export class SearchBar {
    static init() {
        console.log('work you C*&*T')
    }
}

Then accessing it from another file:

import { SearchBar } from './modules/searchBar';

export class Main {
    constructor() {
        SearchBar.init();
    }
}

let main = new Main();
发布评论

评论列表(0)

  1. 暂无评论