Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

visual studio code - Configure Emmet for JSX in VSCode

I use CSS like this:

const styles = {
  foo: {
    color: 'red'
  }
}

<div className={styles.foo} />

and I want emmet to expand .foo to <div className={styles.foo}></div>

I don't see any reference to class or className in emmet's config file.

Also looked into preferences.json and didn't find a solution.

It seems very simple to do.

What am I missing here?

My code editor is vscode.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Emment configuration or enabling is editor specific. In VSCode, You need to enable it for current work space. Follow theses steps. (If you are busy, follow bold letters.)

  1. Press Ctrl + , or Cmd + , or File > Preferences > Settings. This will open Settings window.
  2. Go to Workspace tab > Extensions > Emmet. You'll see Edit in settings.json under Preferences.

enter image description here

  1. You'll see following content by default (my version is 1.35.0) for the new version please see Kevin's comment

    {
        "folders": [],
        "settings": {}
    }
    
  2. Change the content to below

    {
        "folders": [],
        "settings": {
            "emmet.includeLanguages": {
               "javascript": "javascriptreact"
            }
         }
    }
    
  3. Save the file Ctrl + S.

  4. Go to your .jsx file, type .myClass. Press tab. It should expand to following.

    <div className="myClass"></div>
    

EDIT: To obtain {myClass} instead of "myClass"

Currently it's a pending [feature request][3]. But you can go to `VSCodeInstallationDir/resources/app/extensions/emmet` and apply the patch. (using `npm i`)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...