How to develop uniapp using vscode

How to develop uniapp using vscode

Because I have always used vscode to develop front-end projects, now some small programs or h5 projects are developed using uniapp. After experiencing hbuiler for a period of time, I still think vscode is good. The following shares some configurations I developed using vscode. These include uniapp component syntax prompts, uniapp code prompts, and automatic code formatting.

Reference document: https://ask.dcloud.net.cn/article/id-36286__page-2

1. Install vetur

First we need to install the vscode basic vue plugin vetur , which can be installed in the vscode extension

2. Install eslint

Install eslint extension in vscode

3. Configure vscode's setting.json

VSCode extends the settings, click File > Preferences > Settings to open the VSCode configuration file settings.json and add the following configuration

{
   "files.autoSave": "off",
   "eslint.validate": [
       "javascript",
       "javascriptreact",
       "vue-html",
       {
       "language": "vue",
       "autoFix": true
       }
   ],
   "eslint.run": "onSave",
   "editor.tabSize": 2,
   "editor.codeActionsOnSave": {
       "source.fixAll.eslint": true
   }
  }

The same is true for automatically formatting Vue projects. The above is part of the configuration. Do not delete the original configuration.
Notice

Different versions of vscode may have slightly different configurations. If there is a problem with any configuration, vscode will prompt you.

4. Create a uniapp project using vue-cli.

You need to make sure you have installed vue-cli globally. If not, install vue-cli first.
vue create -p dcloudio/uni-preset-vue my-project

insert image description here

We first select the default template (Typescript) of course you can also choose other templates

After the installation is complete, we use vscode to open the project we just created.

5. Install component syntax hints in the project

npm i @dcloudio/uni-helper-json , if it is already installed in your package.json file, you don’t need to install it. At this time, we can see the syntax prompt of the component

insert image description here

6. Install uniapp-snippet plugin in vscode

7. Save and automatically format the code

You can see that the format of the page is quite messy at this time, which is uncomfortable to look at, and it will not be automatically formatted when saved, so we need to add eslint to the project
vue add eslint
We choose the last prettier
After the installation is complete, we can see that our project has some more files, and we can configure the rules in the .eslintrc.js file.

insert image description here

We can see that some configured js files report errors. We can ignore eslint checks at the beginning and end of these files, such as postcss.config.js

/* eslint-disable */
const path = require("path");
module.exports = {
  parser: require("postcss-comment"),
  plugins: [
    require("postcss-import")({
      resolve(id, basedir, importOptions) {
        if (id.startsWith("~@/")) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(3));
        } else if (id.startsWith("@/")) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(2));
        } else if (id.startsWith("/") && !id.startsWith("//")) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1));
        }
        return id;
      },
    }),
    require("autoprefixer")({
      remove: process.env.UNI_PLATFORM !== "h5",
    }),
    require("@dcloudio/vue-cli-plugin-uni/packages/postcss"),
  ],
};
/* eslint-disable */

The other configured js files are similarly operated. After the configuration is completed, we save the code and it will be automatically formatted.

8. Import the code blocks provided by HBuilderX

Download the uni-app code block from github and put it in the .vscode directory under the project directory to have the same code block as HBuilderX. d Code block download address https://github.com/zhetengbiji/uniapp-snippets-vscode

This is the end of this article about how to use vscode to develop uniapp. For more relevant vscode development uniapp content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • uniapp project optimization methods and suggestions
  • The complete code of the uniapp packaged applet radar chart component
  • How to use ECharts in WeChat Mini Programs using uniapp
  • Analyze how uniapp dynamically obtains the interface domain name
  • uniapp dynamic modification of element node style detailed explanation

<<:  The latest super detailed VMware virtual machine download and installation graphic tutorial

>>:  The latest super detailed graphic tutorial of installing Kali Linux on virtual machine VMware

Recommend

Docker connection mongodb implementation process and code examples

After the container is started Log in to admin fi...

Implementation of MySQL scheduled database backup (full database backup)

Table of contents 1. MySQL data backup 1.1, mysql...

How to install and use Ubuntu Docker

Table of contents 1. Automatic installation using...

Shell script nginx automation script

This script can satisfy the operations of startin...

How to install babel using npm in vscode

Preface The previous article introduced the insta...

HTML multimedia application: inserting flash animation and music into web pages

1. Application of multimedia in HTML_falsh animat...

WeChat applet realizes the effect of shaking the sieve

This article shares the specific code of the WeCh...

A brief discussion on macrotasks and microtasks in js

Table of contents 1. About JavaScript 2. JavaScri...

Analysis of two usages of the a tag in HTML post request

Two examples of the use of the a tag in HTML post...

Detailed analysis of MySQL instance crash cases

[Problem description] Our production environment ...

React entry-level detailed notes

Table of contents 1. Basic understanding of React...

Using js to achieve the effect of carousel

Today, let's talk about how to use js to achi...

Detailed explanation of JavaScript primitive data type Symbol

Table of contents Introduction Description Naming...