本文介绍了Javascript导入函数语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从另一个模块导入一个函数,但在运行时遇到错误:
I am trying to import a function from another module but on running am getting an error:
TypeError: _this.getData is not a function.
data.js
function getData() {
return [
{ id: 1,
name: 'Pluto',
type: 'Dwarf Planet'
},
{ id: 2,
name: 'Neptune',
type: 'Planet'
}
]
}
export { getData }
worker.js
worker.js
import getData from data.js
this.data = this.getData()
然后在运行中我得到如上所述的浏览器错误。关于我做错的任何想法?
Then on run I get the browser error as mentioned above. Any ideas as to what I am doing incorrectly?
推荐答案
使用getData()时应省略this关键字它不属于当前对象。
You should omit the "this" keyword when using "getData()" it doesn't belong on the current object.
function getData() { .... }
export default getData;
import getData from "data.js"
this.data = getData();
这篇关于Javascript导入函数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!