Use Typescript configuration steps in Vue

Use Typescript configuration steps in Vue

Through the development of various front-end frameworks, such as vue3.0, react and angular, the source code of frameworks are all written in ts (typescript), so I feel that the development trend of future medium and large projects is also inseparable from ts. Therefore, I wrote documents using vue combined with ts according to some introductory tutorials, which is suitable for getting started with configuring vue+ts projects.

1. TypeScript is introduced into the old Vue project

npm install vue-class-component vue-property-decorator --save
npm install ts-loader typescript tslint tslint-loader tslint-config-standard --save-dev

vue-class-component : extends vue to support typescript, and supports ts in a declarative way

vue-property-decorator : Extend more decorators based on vue-class-component

ts-loader : enables webpack to recognize ts files

tslint-loader : tslint is used to constrain file encoding. It can be installed or not. It is recommended to install it, which is conducive to code standardization.

tslint-config-standard : tslint configures standard style constraints, which is also used to standardize ts code style

注: This way of installing ts is to change the Js syntax in the original vue project to Ts. For detailed steps, please refer to https://www.jb51.net/article/230703.htm. This blog does not fully display the configuration support for ts syntax in vue.config.js or lower versions of webpack.base.conf, so I modified it as follows:

// For file plugin configuration, it needs to be written in the configureWebpack object.
module.exports = {
    configureWebpack: {
        resolve: { extensions: [".ts", ".tsx", ".js", ".json"] },
        module: {
            rules:
                {
                    test: /\.ts$/,
                    exclude: /node_modules/,
                    enforce: 'pre',
                    loader: 'tslint-loader'
                },
                {
                    test: /\.tsx?$/,
                    loader: 'ts-loader',
                    exclude: /node_modules/,
                    options:
                        appendTsSuffixTo: [/\.vue$/],
                    }
                }
            ]
        }
    }
}

Create a Vue+Typescript project from scratch

This method is relatively simple. You can create it by simply selecting Custom when creating a project with the command vue create app-name Follow the steps below:

insert image description here

insert image description here

In the second step, just select the above options. Use the space bar to select them in the terminal, and press Enter after selecting. The meanings of the options are as follows:

 (*) Babel //ES6 to ES5
 (*) TypeScript //Using ts
 ( ) Progressive Web App (PWA) Support //Progressive Web App (*) Router //Routing (*) Vuex //State Management (*) CSS Pre-processors //CSS preprocessing (*) Linter / Formatter //Specification type ( ) Unit Testing //Test ( ) E2E Testing //Test

The next step configuration details are as follows:

Use class-style component syntax? (Y/n) Do you want to use class-style component syntax? Enter Y and press Enter Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, trans
piling JSX)? (Y/n) Do you use Babel and TypeScript (modern mode, automatic detection of polygon fills, trans required (JSX) Enter Y Enter Use history mode for router? (Requires proper server setup for index fallback in product
ion) (Y/n) Do you want to use history routing mode? Enter N and press Enter. Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default) I usually choose Sass/SCSS (with node-sass)

Pick a linter / formatter config: (Use arrow keys): Select the syntax detection specification. Generally, the first one, ESLint with error prevention only, is the default. However, if you use ts, you can choose TSLint

Pick additional lint features: (Press to select, to toggle all, to invert selection) Select Check on Save / Check on Submit Generally, select the first check on save during development Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? (Use arrow keys) Select where to store the configuration information, either separately or in package.json Generally, the first one is selected by default, and the plugin configuration is stored in a separate file Save this as a preset for future projects? (y/N) Do you want to save it as a preset so that you don't have to re-select it next time you create a project? Enter N and press Enter

After completing the above options, the project is built successfully, and the project directory is as follows:

insert image description here

You need to create the vue.config.js file yourself and put it in the root directory of the project.

This is the end of this article about the steps to use typescript configuration in vue. For more relevant vue typescript configuration 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:
  • The simplest tutorial for integrating Vue with TypeScript (recommended)
  • Detailed configuration of using Typescript in Vue2 Vue-cli

<<:  A detailed discussion on detail analysis in web design

>>:  Solve the problem that the VMWare virtual machine centos time is inconsistent with the local time

Recommend

DIV background semi-transparent text non-translucent style

DIV background is semi-transparent, but the words ...

Tips for creating two-dimensional arrays in JavaScript

Creation of a two-dimensional array in Js: First ...

SQL implementation of LeetCode (196. Delete duplicate mailboxes)

[LeetCode] 196.Delete Duplicate Emails Write a SQ...

Linux virtual memory settings tutorial and practice

What is Virtual Memory? First, I will directly qu...

HTML table tag tutorial (24): horizontal alignment attribute of the row ALIGN

In the horizontal direction, you can set the row ...

Vite2.0 Pitfalls

Table of contents Vite project build optimization...

9 Practical Tips for Creating Web Content Pages

Content 1. Give readers a reason to stay. Make the...

Detailed explanation of Vue custom instructions

Table of contents Vue custom directive Custom dir...

js to achieve star flash effects

This article example shares the specific code of ...

A brief discussion on HTML table tags

Mainly discuss its structure and some important pr...

A brief analysis of MySQL explicit type conversion

CAST function In the previous article, we mention...

How to capture exceptions gracefully in React

Table of contents Preface ErrorBoundary Beyond Er...

How to install MySQL database on Debian 9 system

Preface Seeing the title, everyone should be thin...

Introduction to Computed Properties in Vue

Table of contents 1. What is a calculated propert...