javascript - 重新出口声明差异(Javascript)
问题描述
解决方案
我希望这有帮助。在 JS 中导出/导入的方法有很多,也许这个答案太冗长了,但这是我了解模块在 JS 中如何工作的参考。
//MODULES
module.exports //exports the module for use in another program.
require() //imports the module for use in the current program.
//exports / require
//exports (in menu.js file).
let Menu = {}; //Create an object to represent the module.
Menu.specialty = "Roasted Beet Burger with Mint Sauce"; //Add properties or methods to the module object.
module.exports = Menu; //Export the module with module.exports.
//require (in order.js file).
const Menu = require('./menu.js'); //Import the module with require() and assign it to a local variable (the .js extension is optional).
console.log('My order is: ' + Menu.specialty); // Use the module and its properties within a program.
//export default
let Menu = {};
export default Menu; //Uses the JavaScript export statement to export JavaScript objects, functions, and primitive data types.
//import
import Menu from './menu';
//Named exports
let burger = 'test';
export { burger }; //Named exports allow us to export data through the use of variables.
//Named imports
import { burger } from './menu';
//Export named exports
export let fries = "fries"; //They can be exported as soon as they are declared
//Import named imports
import { fries } from 'menu';
//Export assign
let specialty = "specialty";
export { specialty as chefsSpecial }; // The 'as' keyword allows us to give a variable name.
//Import as
import { chefsSpecial as specialForYou } from 'Menu';
//Another way of using aliases is to import the entire module as an alias:
import * as Carte from './menu';
Carte.chefsSpecial;
Carte.isVeg();
Carte.isLowSodium();
推荐阅读
- ios - 应用程序进入后台后的SKDownload问题
- javascript - 具有多个 setstate 的 React js 单个函数
- youtube - youtube dash摄取验证问题
- reactjs - 在组件之间交换数据
- json - 遍历 XML 节点,然后检查是否有匹配的 json 值,然后选择该值
- c++ - Boost Spirit x3 解析为带有跳过成员的结构(如果它为空)
- javascript - 当用户在javascript中退出全屏时,如何取消选中复选框?
- java - 将char转换为int java的问题
- xml - 使用 Typescript 和 xml2js 解析 XML
- javascript - 单击时访问本地存储中的键值未按计划工作