vue项目中使用typescript只需这几步

附上demo地址gitlee

  1. npx tyarn add typescript @vue/cli-plugin-typescript -D

  2. npx tsc --init

  3. 修改 tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "useDefineForClassFields": true,
    "sourceMap": true,
    "allowJs": true,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}
  1. 修改 main.js

将main.js 改为 main.ts

  1. 增加 src/vue-shim.d.ts src/shims-tsx.d.ts
// vue-shim.d.ts
declare module '*.vue' {
  import Vue from 'vue';
  export default Vue;
}
// shims-tsx.d.ts
import Vue, { VNode } from 'vue'

declare global {
  namespace JSX {
    interface Element extends VNode {}
    interface ElementClass extends Vue {}
    interface IntrinsicElements {
      [elem: string]: any
    }
  }
}
  1. 修改 vue.config.js
module.exports = {
  configureWebpack: (config) => {
    config.resolve.extensions.push(...['.ts', '.tsx', '.js', '.json']);
  },
};
  1. 修改组件: AboutView.vue 为例
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
  data() {
    return {
      name: 'about'
    }
  }
})
</script>
  1. 修改组件: HomeView.vue 为例
<script lang="ts">
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";
import Vue, { PropType } from "vue";
interface IObj {
  name: string;
  age: number;
}
export default Vue.extend({
  name: "HomeView",
  components: {
    HelloWorld,
  },
  props: {
    str: {
      type: String,
    },
    obj: {
      type: Object as PropType<IObj>,
    },
    fn: {
      type: Function as PropType<() => void>,
    },
  },
  data() {
    return {
      age: 13,
    };
  },
  methods: {
    getAge(age: string): number {
      return parseInt(age);
    },
  },
});
</script>
  1. 安装eslint插件

npx tyarn add @typescript-eslint/eslint-plugin @typescript-eslint/parser vue-eslint-parser -D

  1. 修改配置文件.eslintrc.js
module.exports = {
  root: true,
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  parser: "vue-eslint-parser",
  extends: [
    'plugin:vue/essential',
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  plugins: ['@typescript-eslint'],
  parserOptions: {
    parser: '@typescript-eslint/parser' // 解析 .ts 文件
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
  }
}
  1. 重启

yarn serve

or

npm run serve

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值