Vue3全家桶-Vite-TS-TSX尝鲜先人一步

13次阅读

共计 4447 个字符,预计需要花费 12 分钟才能阅读完成。

Vue3 与 TSX 尝鲜版

波及到的次要依赖

  1. vite@1.0.0-beta.11:新一代脚手架
  2. vue@3.0.0-beta.22:beta 版
  3. vuex@4.0.0-beta.4
  4. vue-router@4.0.0-beta.2
  5. typescript@3.9.6

筹备工作

  1. 确保装置yarn
npm install yarn -g
  1. 确保装置 vite 脚手架
npm install -g create-vite-app
# or
yarn add -g create-vite-app

开始

我的项目初始化

yarn create vite-app <project-name>

集成 TS

yarn add --dev typescript

我的项目根目录创立配置文件:tsconfig.json

{"include": ["./**/*.ts"],
  "compilerOptions": {
    "jsx": "react",
    "target": "es2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
    // "lib": ["es2017.object"] /* Specify library files to be included in the compilation. */,
    // "declaration": true /* Generates corresponding '.d.ts' file. */,
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    "sourceMap": true /* Generates corresponding '.map' file. */,
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    "outDir": "./dist" /* Redirect output structure to the directory. */,

    "strict": true /* Enable all strict type-checking options. */,
    "noUnusedLocals": true /* Report errors on unused locals. */,
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,

    "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  }
}

集成 eslint

yarn add --dev eslint eslint-plugin-vue

我的项目根目录创立配置文件.eslintrc.js

module.exports = {
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser', // Specifies the ESLint parser
    ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
    sourceType: 'module', // Allows for the use of imports
    ecmaFeatures: {
      // tsx: true, // Allows for the parsing of JSX
      jsx: true,
    },
  },
  // settings: {
  //   tsx: {
  //     version: "detect" // Tells eslint-plugin-react to automatically detect the version of React to use
  //   }
  // },
  extends: [
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
    'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
    'plugin:prettier/recommended', // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
  ],
  rules: {
    // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
    // e.g. "@typescript-eslint/explicit-function-return-type": "off",
  },
};

集成 pritter

yarn add --dev prettier eslint-config-prettier eslint-plugin-prettier

我的项目根目录创立配置文件:.prettierrc.js

module.exports = {
  semi: true,
  trailingComma: "all",
  singleQuote: true,
  printWidth: 100,
  tabWidth: 2,
  endOfLine:"auto"
};

到这一步,一个 Vue3+TSX 的我的项目就搭建起来了,以上配置文件的具体内容就不做解释了。

批改入口文件

因为默认我的项目模板是以 src/main.js 为入口的,咱们须要把它批改为 src/main.ts
根目录的 index.html中批改入口文件的援用即可:

... ...
<body>
  ... ...
  <script type="module" src="/src/main.ts"></script>
</body>
</html>

优化 TS 类型推断

在 src 目录下,创立shim.d.ts、source.d.ts

shim.d.ts: (这个其实不太须要,因为我的项目中全是通过 tsx 开发的)

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

source.d.ts: (优化编译器提醒,申明动态资源文件)

declare const React: string;
declare module '*.json';
declare module '*.png';
declare module '*.jpg';

集成 vue-router

yarn add --dev vue-router@4.0.0-beta.2

这里能够去 npm 官网 查找最新版本
在 src 目录下,新建 router 文件夹 ,并在文件夹内 创立 index.ts
index.ts:

import {RouteRecordRaw, createRouter, createWebHistory} from 'vue-router';

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: import('../views/Home'),
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About'),
  },
];

const router = createRouter({history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;

这里创立 router 的形式与之前不同,在 vue3 中,联合 TS 的类型推断,开发效率会高很多。

集成 vuex

yarn add --dev vuex@4.0.0-beta.4

在 src 目录下,新建 store 文件夹,并在文件夹内创立index.ts

index.ts:

import {state} from './state';
import {createStore} from 'vuex';

export default createStore({
  state,
  mutations: {},
  actions: {},
  modules: {},});

state.js:

export interface State {title: string;}

export const state: State = {title: 'Vue(v3) 与 tsx 的联合~',
};

main.ts

最终 main.ts 中引入 store、router:

import {createApp} from 'vue';
import App from './App';
import router from './router';
import store from './store';

createApp(App).use(router).use(store).mount('#app');

结尾

vue3 正式版的公布,势必导致 vue2 的周边框架的个体更新,例如 UI 框架、基于 Vue2 的指令库等,作为这么久的白嫖党,也要为 vue3 社区的建设出一份力了。

Vue3 与 TS 的联合是大趋势,如果不适应 TS,那还是倡议应用 Vue2 吧。23333~

后续博主也将钻研 vite 框架和 vue3 全家桶的新个性与 API,争取输入有品质的文档。


最初附上源码地址:https://github.com/justwiner/vue3-tsx


参考文章:https://github.com/hyperMoss/vue-tsx

正文完
 0