如何在import语句中使用模板文字? [英] How do I use a template literal in an import statement?

查看:49
本文介绍了如何在import语句中使用模板文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行此行时:

import stats from `./${process.env.STATS}`

报告以下错误:

解析错误:意外的令牌`

Parsing error: Unexpected token `

可以使用以下表达式成功加载模块:

The module can be loaded successfully with the expression:

const stats = require(`./${process.env.STATS}`);

import 语句似乎需要一个常规字符串,因为它与该语句一起工作:

The import statement seems to require a regular string as it works with the statement:

import stats from './statsdir'

其中'./statsdir' process.env.STATS 的值.

为什么会发生错误?

推荐答案

为什么会发生错误?

Why does the error occur?

看来您自己找到了答案:

It seems you found the answer yourself:

import 语句似乎需要常规字符串

The import statement seems to require a regular string

完全正确. import 需要一个字符串 literal .导入位置不能是动态的.

Exactly. import needs a string literal. It import location cannot be dynamic.

相关: node.js中的ES6变量导入名称?

这篇关于如何在import语句中使用模板文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆