我能够将vanilla.js文件导入到我的使用import './vanilla';
的本机项目中
当我尝试使用该文件中的任何内容时,它只写着Can't find variable
。我知道导入是成功的,因为如果我将window.something
放入普通文件中,就可以调用something
。因此,我想问题是如何获得从vanilla文件导入的变量、函数等的可见性?
发布于 2016-12-08 07:13:30
你应该能够做到这一点:
import vanilla from './vanilla';
然后可以使用导出的函数,即:
vanilla.someMethod();
发布于 2016-12-08 07:04:18
这在很大程度上取决于如何导出模块?您有多个选项来导出模块或该模块的功能。
例如:
// export data
export var color = "red";
export let name = "Nicholas";
export const magicNumber = 7;
// export function
export function sum(num1, num2) {
return num1 + num1;
}
// export class
export class Rectangle {
constructor(length, width) {
this.length = length;
this.width = width;
}
}
// this function is private to the module
function subtract(num1, num2) {
return num1 - num2;
}
// define a function...
function multiply(num1, num2) {
return num1 * num2;
}
// ...and then export it later
export { multiply };
或导出默认值,例如:
function sum(num1, num2) {
return num1 + num2;
}
export default sum;
您可以在这里阅读有关导出和导入模块的更多信息:https://leanpub.com/understandinges6/read/#leanpub-auto-basic-exporting
https://stackoverflow.com/questions/41042410
复制相似问题