[英]CommonJS alternative to ES6's importing as alias
ES6 可以像這樣導入導出為 alias ;
import express from 'express'
import { express as playground } from 'graphql-playground/middleware'
有沒有其他方法可以用 CommonJS require('something')
做到這require('something')
? 或者,如果以 CommonJS 的方式完成,可以繞過上述聲明問題?
這會引發錯誤。
const express = require('express')
const express = require('graphql-playground/middleware')
// SyntaxError: Identifier 'express' has already been declared
CommonJS 實際上只是給變量賦值,你可以隨意命名變量:
const express = require('express');
const playground = require('graphql-playground/middleware').express;
對於非默認導出(module.export = var),您還可以使用常規解構語法進行別名:
const {
originName: newNameInFile
} = require('foo.js')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.