简体   繁体   中英

visual studio code React does not recognize html

如您所见,HTML 内容不是彩色的,并且无法自动完成 HTML 标记

There are 2 ways to do that.

1) You could manually set the syntax to "Javascript React".

For that click on "Javascript" on the bottom right of your screen:

在此处输入图片说明

Then simply enter "react" and select it. After that you should have autocomplete for the html parts inside your render functions.

2) You can instruct VSCode to always open .js files as "javascriptreact". For that go to your settings and copy files.associations over to your local settings. Make sure it looks something like this and save:

"files.associations": { "*.js": "javascriptreact" }

Note on that one: This will always set the syntax to "javascriptreact" when you open .js files and might not always be what you want. A better solution would be to generally name react files as myfile.jsx . This way vscode will automatically set the syntax to "javascriptreact".

Hope this helps.

  • Save the file as .jsx ie (App.jsx) instead of .js

This is because you are saving the file as .js ie App.js.JavaScript files cannot understand HTML tags.

Another alternative is you can save in (.js) but your web pack should be configured in such a way that it should transpile it into .jsx. For doing refer this - https://github.com/facebook/create-react-app

React 只理解 javascript xml。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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