问题描述
我有两个文件,一个是名为 app.js
的主 js 文件,还有一个文件,用于存储我所有的 js 函数,名为 functions.js
。如下图所示。
但我想将 functions.js
文件包含到 app.js
文件中。所以我用谷歌搜索了如何做到这一点,这就是人们所说的:
但是我的 npm run dev 说该文件不存在。但是路径是正确的。我在这里做错了什么,还有其他方法吗?
解决方法
您可以简单地在任何您想创建的地方创建文件,然后导出一些属性或方法,然后将它们导入您的 app.js file
或您需要的任何文件中。像这样:
//new_file.js
export const jokes = function() {
return ['funny','not really funny','boring']
}
export const heading = 'some global heading to be reused.'
在你的 app.js 文件中:
import { jokes,heading } from 'new_file.js'//specify actual path here .
console.log(jokes) // ['funny','boring']
console.log(heading)//some global heading to be reused.
This tutorial might be helpful too .
http://www.2ality.com/2014/09/es6-modules-final.html