简体   繁体   中英

Vue.js + TypeScript importing plain js function

I am trying to import external JS function from separate file into my Vue.js app which is running on TypeScript.

products_table.js

function init_products_table() {
    console.log('test')
}

export {init_products_table as initProductsTable}

And I import it into my Vue component like this:

import { initProductsTable } from '../../products_table.js'

And I get this compile error:

TS7016: Could not find a declaration file for module '../../products_table.js'.

First if you use TS with Vue.JS you should enable JS in your 'tsconfig.json' file. After some recommendation:

  • importing a JS file into TS is a bit counterproductive, I advise you to code your external function in TS directly, this can avoid future conflicts.
  • when importing you don't have to give the file type. Here '../../products_table.js' would be '../../products_table'.

If the problem persists please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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