前言
假设已经有一个通过 vue-cli3 脚手架构建的 vue 项目
命令行安装 Typescript
npm install --save-dev typescript npm install --save-dev @vue/cli-plugin-typescript
编写 Typescript 配置
根目录下新建 tsconfig.json,下面为一份配置实例(点击查看所有配置项)。值得注意的是,默认情况下,ts 只负责静态检查,即使遇到了错误,也仅仅在编译时报错,并不会中断编译,最终还是会生成一份 js 文件。如果想要在报错时终止 js 文件的生成,可以在 tsconfig.json 中配置 noEmitOnError 为 true。
{ "compilerOptions": { "target": "esnext", "module": "esnext", "strict": true, "importHelpers": true, "moduleResolution": "node", "experimentalDecorators": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "sourceMap": true, "baseUrl": ".", "allowJs": false, "noEmit": true, "types": [ "webpack-env" ], "paths": { "@/*": [ "src/*" ] }, "lib": [ "esnext", "dom", "dom.iterable", "scripthost" ] }, "exclude": [ "node_modules" ] }
新增 shims-vue.d.ts
根目录下新建 shims-vue.d.ts,让 ts 识别 *.vue 文件,文件内容如下
declare module '*.vue' { import Vue from 'vue' export default Vue }
修改入口文件后缀
src/main.js => src/main.ts
改造 .vue 文件
.vue 中使用 ts 实例
// 加上 lang=ts 让webpack识别此段代码为 typescript <script lang="ts"> import Vue from 'vue' export default Vue.extend({ // ... }) </script>
一些好用的插件
vue-class-component:强化 Vue 组件,使用 TypeScript装饰器 增强 Vue 组件,使得组件更加扁平化。点击查看更多
import Vue from 'vue' import Component from 'vue-class-component' // 表明此组件接受propMessage参数 @Component({ props: { propMessage: String } }) export default class App extends Vue { // 等价于 data() { return { msg: 'hello' } } msg = 'hello'; // 等价于是 computed: { computedMsg() {} } get computedMsg() { return 'computed ' + this.msg } // 等价于 methods: { great() {} } great() { console.log(this.computedMsg()) } }
vue-property-decorator:在 vue-class-component 上增强更多的结合 Vue 特性的装饰。点击查看更多
import { Vue, Component, Prop, Watch, Emit } from 'vue-property-decorator' @Component export default class App extends Vue { @Prop(Number) readonly propA: Number | undefined @Prop({ type: String, default: ''}) readonly propB: String // 等价于 watch: { propA(val, oldval) { ... } } @Watch('propA') onPropAChanged(val: String, oldVal: String) { // ... } // 等价于 resetCount() { ... this.$emit('reset') } @Emit('reset') resetCount() { this.count = 0 } // 等价于 returnValue() { this.$emit('return-value', 10, e) } @Emit() returnValue(e) { return 10 } // 等价于 promise() { ... promise.then(value => this.$emit('promise', value)) } @Emit() promise() { return new Promise(resolve => { resolve(20) }) } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】
- 【古琴音乐】管平湖《广陵散》2004[FLAC+CUE]
- 【中国民乐】《少数民族器乐·苗岭的早晨》2004[WAV+CUE]
- 【中国音乐】盛中国《新疆之春》2004[FLAC+CUE]
- 群星《音你而来 第8期》[320K/MP3][44.64MB]
- 群星《音你而来 第8期》[FLAC/分轨][121.21MB]
- 群星《十二女声Ⅲ》[WAV/CUE/分轨][626.4MB]
- 陈百强.1993-离不开...纪念歌集2CD【华纳】【WAV+CUE】
- 于台烟.2003-爱自己【乾坤唱片】【WAV+CUE】
- 群星.2024-我们永远是我们电影原声带【乐禾发文化】【FLAC分轨】
- 【中国民乐】张维良《秋江夜泊》2004[FLAC+CUE]
- 明达年度发烧碟MasterSuperiorAudiophile2011[DSF]