The installation
-
Create a vUE project file with vue-Router reserved, see vue init webpack vue-typescript-Template
-
NPM install –save-dev typescript ts-loader
-
Install tsLint static syntax check: NPM I –save-dev tslint tslint-loader tslint-config-standard
-
Run, access the host test: NPM run dev
configuration
build/webpack.base.conf.js
The:
entry: {
app: './src/main.js'
}
Copy the code
To:
entry: {
app: './src/main.ts'
}
Copy the code
Change the file name of SRC /index.js to the corresponding SRC /index.ts.
build/webpack.base.conf.js
In theresolve.extensions
To:
resolve: {
extensions: ['.ts'.'.js'.'.vue'.'.json'].alias: {
'vue$': 'vue/dist/vue.esm.js'.The '@': resolve('src'),}}Copy the code
- configuration
ts-loader
.build/webpack.base.conf.js
In themodule.rules
Add:
{
test: /\.tsx? $/.use: [{
loader: 'babel-loader'
}, {
loader: 'ts-loader'.options: {
appendTsSuffixTo: [/\.vue$/]}}],exclude: /node_modules/
}
Copy the code
- Add in the project root directory
tsconfig.json
The content of the file is:
{
"compilerOptions": {
"strict": true."module": "es2015"."moduleResolution": "node"."target": "es6"."allowSyntheticDefaultImports": true."lib": [
"es2017"."dom"]},"include": [
"./src/*"]}Copy the code
- Run the test
npm run dev
If the console reports this error:
error in ./src/main.ts
Module build failed: TypeError: Cannot read property 'afterCompile' of undefined
at successfulTypeScriptInstance (/Users/baidu/Github/Vue-TypeScript-Template/node_modules/ts-loader/dist/instances.js:147:28)
at Object.getTypeScriptInstance (/Users/baidu/Github/Vue-TypeScript-Template/node_modules/ts-loader/dist/instances.js:48:12)
at Object.loader (/Users/baidu/Github/Vue-TypeScript-Template/node_modules/ts-loader/dist/index.js:16:41) @ multi (webpack)-dev-server/client? http://localhost:8080 webpack/hot/dev-server ./src/main.ts
Copy the code
Official issue of TS-Loader, modify version “TS-loader “: “3.5.0” in package.json, reinstall it, but this is also a temporary solution, remember to upgrade after the official compatibility is resolved.
- configuration
tslint
In thebuild/webpack.base.conf.js
In themodule.rules
Add:
{
test: /\.tsx? $/.// tsLint code checks to open comments available
loader: 'tslint-loader'.enforce: 'pre'.include: [resolve('src'), resolve('test')].exclude: /node_modules/
}
Copy the code
build/vue-loader.conf.js
Revised in the document:
// ...
const merge = require('webpack-merge')
module.exports = {
loaders: merge(utils.cssLoaders({
sourceMap: sourceMapEnabled,
extract: isProduction
}), {
ts: ['ts-loader'.'tslint-loader']}),// ...
}
Copy the code
- in
src/
Add to foldertslint.json
The content of the file is:
{
"extends": "tslint:recommended"."globals": {
"require": true
},
"rulesDirectory": ["src/"]."rules": {
"no-consecutive-blank-lines": false."object-literal-sort-keys": false."no-trailing-whitespace": false."no-unused-expression": [true."allow-new"]}}Copy the code
Code changes
- in
src/
File creationvue-shims.d.ts
The content of the file is:
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
Copy the code
-
SRC/all.js in the file changed to.ts
-
The SRC/app. vue file is changed to:
<script lang="ts">
import Vue from "vue";
const App = Vue.extend({
name: "App"});export default App;
</script>
Copy the code
Class vue.extend ({}) is created by adding
- Modify all pairs
*.vue
Document reference:
import App from "./App";
import HelloWorld from ".. /components/HelloWorld";
Copy the code
To:
import App from "./App.vue";
import HelloWorld from "@/components/HelloWorld.vue";
Copy the code
All references to *. Vue end with *. Vue
Run the test
Run NPM run dev to test the effect
conclusion
The above configuration process is a successful process of adding typescript, TS-Loader, tsLint to a VUE project after it is created based on the official VUE scaffolding. Vuex will be added later, and vuE-class-Component, vuex-class, vuE-property-decorator will be incorporated into the project for further implementation.
Github
Project reference Address: github.com/liuchunhui/…