前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在vue中使用ts

在vue中使用ts

作者头像
阿超
发布2022-08-21 11:02:45
7390
发布2022-08-21 11:02:45
举报
文章被收录于专栏:快乐阿超快乐阿超

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

寻找到vue.js

image-20220103120155111
image-20220103120155111

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

首先新建一个空项目

image-20220103121442100
image-20220103121442100

然后新建src目录和components目录

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

输入命令下载依赖

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

很快就下载完毕

image-20220103121921145
image-20220103121921145

然后初始化ts环境

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

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

image-20220103122054083
image-20220103122054083

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

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

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

然后我们再新建一个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"
	}
}

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

image-20220103124616317
image-20220103124616317

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

代码语言:javascript
复制
D:\file\projects\vue-ts-demo>cnpm run build

> @ build D:\file\projects\vue-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:\Users\achao\AppData\Roaming\npm-cache\_logs\2022-01-03T04_44_20_952Z-debug.log

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

image-20220103130312281
image-20220103130312281

再来,发现报错变了

它说是找不到这个文件

image-20220103130410439
image-20220103130410439

那我们新建一个,这就是我们的主要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"
    }
});
image-20220103130518049
image-20220103130518049

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

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

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

我们顺便加上

image-20220103131136245
image-20220103131136245
image-20220103131145898
image-20220103131145898

警告消失

image-20220103131207600
image-20220103131207600

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

image-20220103131332747
image-20220103131332747
代码语言: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>
image-20220103131804180
image-20220103131804180

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中使用一下,看看效果

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

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

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

</html>

效果如下:

image-20220103133838475
image-20220103133838475

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

我们去修改下webpack.config.js

image-20220103134439850
image-20220103134439850

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

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档