简体   繁体   English

如何在 angular 中调用 js 文件的函数到打字稿组件

[英]How to invoke a function of a js file to a typescript component in angular

I have placed the js file in assets folder and imported it in index.html.我已将 js 文件放在 assets 文件夹中并将其导入到 index.html 中。

I need to use the functions of js file in angular component我需要在angular组件中使用js文件的功能

via.js通过.js

var _via_reg_canvas = document.getElementById('region_canvas');
function _via_init() {
       show_message(VIA_NAME + ' (' + VIA_SHORT_NAME + ') version ' + VIA_VERSION +
        '. Ready !', 2 * VIA_THEME_MESSAGE_TIMEOUT_MS);

    if (_via_is_debug_mode) {
        document.getElementById('ui_top_panel').innerHTML += '<span>DEBUG MODE</span>';
    }

    document.getElementById('img_fn_list').style.display = 'block';
    document.getElementById('leftsidebar').style.display = 'table-cell';

    project_init_default_project();

  _via_init_reg_canvas_context();
  _via_init_keyboard_handlers();
  _via_init_mouse_handlers();
  image_grid_init();

  show_single_image_view();
  init_leftsidebar_accordion();
  attribute_update_panel_set_active_button();
  annotation_editor_set_active_button();
  init_message_panel();
}

view.ts视图.ts

inside this ts , I want to use _via_init() function which is from via.js

You need to export the function from your js file.您需要从 js 文件中导出该函数。

export function _via_init() {
       ...
}

Now in your view.ts you simply import it like:现在在您的 view.ts 中,您只需将其导入:

import * as via from 'path/to/assets/via.js'

And use it like:并像这样使用它:

via._via_init();

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

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