在vue中使用ts

2022-08-21 11:02:45 浏览数 (1)

首先找到文档:https://www.tslang.cn/samples/index.html

寻找到vue.js

跳转过去后就是教程,我们跟着教程一步一步来

首先新建一个空项目

然后新建src目录和components目录

代码语言:javascript复制
typescript-vue-tutorial/
├─ dist/
└─ src/
   └─ components/

输入命令下载依赖

代码语言:javascript复制
D:fileprojectsvue-ts-demo>cnpm install --save-dev typescript webpack webpack-cli ts-loader css-loader vue vue-loader vue-template-compiler

很快就下载完毕

然后初始化ts环境

代码语言:javascript复制
tsc --init

可以看到多出了ts配置文件

我们可以在这里看到全部配置,我们可以手动对齐进行更改

也可以直接使用官方提供的配置

代码语言:javascript复制
{
    "compilerOptions": {
        "outDir": "./built/",
        "sourceMap": true,
        "strict": true,
        "noImplicitReturns": true,
        "module": "es2015",
        "moduleResolution": "node",
        "target": "es5"
    },
    "include": [
        "./src/**/*"
    ]
}

然后我们再新建一个webpack.config.js到根目录下:

代码语言:javascript复制
var path = require('path')
var webpack = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
  entry: './src/index.ts',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax',
          }
          // other vue-loader options go here
        }
      },
      {
        test: /.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/.vue$/],
        }
      },
      {
        test: /.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map',
  plugins: [
    // make sure to include the plugin for the magic
    new VueLoaderPlugin()
  ]
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

再到package.json中添加构建配置

代码语言:javascript复制
{
	"devDependencies": {
		"css-loader": "^6.5.1",
		"ts-loader": "^9.2.6",
		"typescript": "^4.5.4",
		"vue": "^2.6.14",
		"vue-loader": "^15.9.8",
		"vue-template-compiler": "^2.6.14",
		"webpack": "^5.65.0",
		"webpack-cli": "^4.9.1"
	},
	"scripts": {
		"build": "webpack",
		"test": "echo "Error: no test specified" && exit 1"
	}
}

我尝试构建,但发现以下错误

报错日常了属于是,仔细看,它说:

代码语言:javascript复制
D:fileprojectsvue-ts-demo>cnpm run build

> @ build D:fileprojectsvue-ts-demo
> webpack

[webpack-cli] 无效的 配置 对象. Webpack 已经 被 序列化了 使用 一个 配置 类 为 并 不 匹配 它的 API 模式.
 - configuration.devtool 应该 满足 表达式 "^(inline-|hidden-|eval-)?(nosources-)?(cheap-(module-)?)?source-map$".
   突破 变更 自从 webpack 5: 这个 devtool 选项 变得 更加 严格.
   Please strictly follow the order of the keywords in the pattern.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! @ build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the @ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:UsersachaoAppDataRoamingnpm-cache_logs2022-01-03T04_44_20_952Z-debug.log

所以我们将webpack.config.js中的configuration.devtool改为表达式中的eval-nosources-cheap-module-source-map

再来,发现报错变了

它说是找不到这个文件

那我们新建一个,这就是我们的主要ts文件

代码语言:javascript复制
// src/index.ts

import Vue from "vue";

let v = new Vue({
    el: "#app",
    template: `
    <div>
        <div>Hello {{name}}!</div>
        Name: <input v-model="name" type="text">
    </div>`,
    data: {
        name: "World"
    }
});

添加完成之后我们再次构建,发现成功构建啦!

代码语言:javascript复制
# 编译
cnpm run build
# 编译并热更新
npm run build -- --watch

这里还有个警告,说我们缺少了mode属性

我们顺便加上

警告消失

接下来我们添加一个组件到src/components

代码语言:javascript复制
// src/components/Hello.ts

import Vue from "vue";

export default Vue.extend({
    template: `
        <div>
            <div>Hello {{name}}{{exclamationMarks}}</div>
            <button @click="decrement">-</button>
            <button @click="increment"> </button>
        </div>
    `,
    props: ['name', 'initialEnthusiasm'],
    data() {
        return {
            enthusiasm: this.initialEnthusiasm,
        }
    },
    methods: {
        increment() { this.enthusiasm  ; },
        decrement() {
            if (this.enthusiasm > 1) {
                this.enthusiasm--;
            }
        },
    },
    computed: {
        exclamationMarks(): string {
            return Array(this.enthusiasm   1).join('!');
        }
    }
});

index.ts中引用

代码语言:javascript复制
// src/index.ts

import Vue from "vue";
import HelloComponent from "./components/Hello";

let v = new Vue({
    el: "#app",
    template: `
    <div>
        Name: <input v-model="name" type="text">
        <hello-component :name="name" :initialEnthusiasm="5" />
    </div>
    `,
    data: { name: "World" },
    components: {
        HelloComponent
    }
});

注意到此为止我们都是使用ts文件进行编写vue代码,如果我们需要使用vue文件,则需要在src下新建vue-shims.d.ts

代码语言:javascript复制
// src/vue-shims.d.ts

declare module "*.vue" {
    import Vue from "vue";
    export default Vue;
}

我们的组件就可以改为Hello.vue

代码语言:javascript复制
<!-- src/components/Hello.vue -->

<template>
    <div>
        <div class="greeting">Hello {{name}}{{exclamationMarks}}</div>
        <button @click="decrement">-</button>
        <button @click="increment"> </button>
    </div>
</template>

<script lang="ts">
import Vue from "vue";

export default Vue.extend({
    props: ['name', 'initialEnthusiasm'],
    data() {
        return {
            enthusiasm: this.initialEnthusiasm,
        }
    },
    methods: {
        increment() { this.enthusiasm  ; },
        decrement() {
            if (this.enthusiasm > 1) {
                this.enthusiasm--;
            }
        },
    },
    computed: {
        exclamationMarks(): string {
            return Array(this.enthusiasm   1).join('!');
        }
    }
});
</script>

<style>
.greeting {
    font-size: 20px;
}
</style>

index.tsHelloComponent我再给个后缀

代码语言:javascript复制
// src/index.ts

import Vue from "vue";
import HelloComponent from "./components/Hello.vue";

let v = new Vue({
    el: "#app",
    template: `
    <div>
        Name: <input v-model="name" type="text">
        <hello-component :name="name" :initialEnthusiasm="5" />
    </div>
    `,
    data: { name: "World" },
    components: {
        HelloComponent
    }
});

我们使用cnpm run build生成的build.js我们可以放到一个html中使用一下,看看效果

代码语言:javascript复制
<!doctype html>
<html>

<body>
    <div id="app"></div>
</body>

<script src="./dist/build.js"></script>

</html>

效果如下:

但这和我们一般vue开发还有点差别,我们开发时应该还有热重载…

我们去修改下webpack.config.js

然后重新运行cnpm run build -- --watch

然后我们打开页面http://127.0.0.1:8848/vue-ts-demo/index.html

我们修改后再次保存,就可以实时更新了

0 人点赞