简体   繁体   English

react-codemirror2 中的 require 与 import 语法

[英]require vs import syntax in react-codemirror2

Totally confused by this example in react-codemirror2 which uses both import and require syntax;完全被 react-codemirror2 中的这个例子搞糊涂了,它同时使用了importrequire语法; [See https://github.com/scniro/react-codemirror2 in the section requiring codemirror resources ] requiring codemirror resources的部分见https://github.com/scniro/react-codemirror2】

import CodeMirror from 'react-codemirror2'; 
require('codemirror/mode/xml/xml'); 
require('codemirror/mode/javascript/javascript');

I know the require('codemirror/etc') stuff is referring to a plain JS library dependency (not React specific), but why does require even work without throwing an error?我知道 require('codemirror/etc') 的东西是指一个普通的 JS 库依赖项(不是 React 特定的),但是为什么require甚至可以工作而不抛出错误?

Can the require be replaced by an import ? require可以替换为import吗? How to make the warning in VS Code editor go away?如何使 VS Code 编辑器中的警告消失? 在此处输入图片说明

The main idea of this part of the Docs is that you need to require by yourself CodeMirror modules that you needed (it means that if you need a javascript mode or autocomplete, you need to add these modules to your code).这部分文档的主要思想是你需要自己 require 你需要的 CodeMirror 模块(这意味着如果你需要一个 javascript 模式或自动完成,你需要将这些模块添加到你的代码中)。

react-codemirror2 doc says that: react-codemirror2 doc 说:

How to import/require these assets will vary according to the specifics of your development environment如何导入/要求这些资产将根据您的开发环境的具体情况而有所不同

In most cases, both require and import will do the same, so you can use the code that is given in react-codemirror2 docs:在大多数情况下, requireimport都会执行相同的操作,因此您可以使用react-codemirror2文档中给出的代码:

import CodeMirror from 'react-codemirror2'; 
require('codemirror/mode/xml/xml'); 
require('codemirror/mode/javascript/javascript');

or use its analog with imports:或将其模拟与导入一起使用:

import CodeMirror from 'react-codemirror2'; 
import 'codemirror/mode/xml/xml'; 
import 'codemirror/mode/javascript/javascript';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM