VUE2项目使用typescript

一、项目兼容typescript

  1. 安装ts-loader,避免loader安装过高版本,添加了版本控制。npm i ts-loader@8.2.0 typescript --save-dev
  2. chainWebpack添加对应loader配置。
config.module
    .rule('tsx')
    .test(/.tsx?$/)
    .use('ts-loader')
    .loader('ts-loader')
    .options({
        appendTsSuffixTo: [/.vue$/]
    })
    .tap(options => {
        return options;
    });

3. 项目根目录添加tsconfig.json文件。

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": "node",
    "lib": ["dom","es2016"],
    "baseUrl": "./",
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "src/**/*.d.ts"
  ],
  "exclude": ["node_modules"]
}

4. 重启项目,让webpack配置生效,我们即可新建ts文件,使用ts了。

a、新建ts-test.ts文件;

b、文件编写一个class测试;

     export class Person {
         age: number = 18
         name: string = '张三'
     }

c、文件引入class,并使用;

    // 引入
    import {Person} from './ts-test.ts';
    // 使用
    const p = new Person()
    console.log(p)

d、程序正常运行。

![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/dea4d99cc2c448e1933619d7eeb4dec7~tplv-k3u1fbpfcp-watermark.image?)

二、vue组件使用typescript编写

  1. 安装插件 npm i vue-class-component vue-property-decorator --save

  2. vue组件使用ts,这里仅写一个简单示例,详细使用可以参考:https://xie.infoq.cn/article/00845440bed4248cb80c15128

    //模板和原生vue保持一致
    <template>
        <div class="container">
            <div>{{message}}</div>
            <div>年龄:{{info.age}}</div>
            <div>年龄:{{info.age}}</div>ming chegn
        </div>
    </template>
    
    <script lang="ts">
        import { Vue, Component } from 'vue-property-decorator';
        type User = {
            name: string;
            age: number;
        };
        @Component({
            components: {
            },
        })
        export default class White extends Vue {
            message = 'ts hello world';
            info: User = { name: '张三', age: 18 };
            created() {
                console.log('created');
            }
            mounted() {
                console.log('mounted');
            }
        }
    </script>
    

    在这里插入图片描述

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中,使用TypeScript可以通过以下几个步骤来实现。首先,你需要确保你的项目已经安装了Vue 3的依赖。然后,你可以使用Vue CLI创建一个新的Vue 3项目,并选择TypeScript作为项目的语言。这将自动生成一个包含TypeScript配置的Vue 3项目。接下来,你可以在Vue组件中使用TypeScript来定义props、data、computed属性以及方法等。使用TypeScript可以增加代码的可读性和可维护性,并提供类型检查的功能,可以帮助你在编码时发现和修复潜在的错误。此外,你还可以使用装饰器来增强Vue组件,让其支持TypeScript装饰器。一个常用的库是vue-class-component,它提供了一些装饰器,例如@Component和@Prop等,用于增强Vue组件的功能并支持TypeScript。通过使用这些装饰器,你可以更好地组织和管理你的Vue组件代码,并提高开发效率。总而言之,在Vue 3中使用TypeScript可以帮助你提高代码质量和开发效率,并且提供了更好的类型检查和维护性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3.0+TS 项目上手教程](https://blog.csdn.net/Ed7zgeE9X/article/details/109039793)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值