尝鲜vue3.0-tyepscript开发组件(3)

4,331 阅读2分钟

背景

延续前面的两篇文章:

第三篇新鲜出炉,主要是说一下,官网推荐vite与之前上的用法区别,以及使用typescript实战一个类element-ui的Dialog组件,使用较高级的Vue3.0的API~_~

Vite

  1. Vite是一个web开发构建工具,它在开发期间通过本地ES模块导入为您的代码提供服务,并将其与Rollup捆绑在一起用于生产。
  • 闪电般的冷服务器启动
  • 快速热模更换(HMR)
  • 真正的随需应变的编译 实际上就是先启动服务(koa),如果你需要某一个文件,再进行编译。这就造成Vite启动服务的速度比Webpack快了很多,即“按需编译”。
  1. 需要我们注意的是:
  • TypeScript已经内置支持,直接就可以使用
  • less/sass/stylus/postcss也内置支持(单需要单独安装所对应的编译器)
  • git地址
  1. 使用vite初始化项目
  npm init vite-app <project-name>
  cd <project-name>
  npm install
  npm run dev

安装配置

  1. 安装sass,eslint,prettier(注意版本号) //.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 tsx
      // jsx: true,// Allows for the parsing of JSX
    },
  },
  plugins: ['@typescript-eslint'],
  extends: [
    'plugin:@typescript-eslint/recommended',
    'plugin:vue/vue3-recommended',
    '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: {
    // js/ts
    'eol-last': 'error',
    'no-trailing-spaces': 'error',
    'comma-style': ['error', 'last'],
    'comma-dangle': ['error', 'always-multiline'],
    quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: true }],
    camelcase: ['error', { properties: 'never' }],
    semi: ['error', 'never'],
    indent: ['error', 2, { SwitchCase: 1 }],
    'object-curly-spacing': ['error', 'always'],
    'arrow-parens': ['error', 'as-needed'],
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/member-delimiter-style': [
      'error',
      {
        multiline: {
          delimiter: 'none',
          requireLast: false,
        },
        singleline: {
          delimiter: 'semi',
          requireLast: true,
        },
      },
    ],
    // vue
    'vue/no-v-html': 'off',
    'vue/singleline-html-element-content-newline': 'off',
    'vue/html-self-closing': [
      'error',
      {
        html: {
          void: 'never',
          normal: 'never',
          component: 'always',
        },
      },
    ],
    'vue/max-attributes-per-line': [
      'error',
      {
        singleline: 3,
        multiline: 1,
      },
    ],
    'vue/require-default-prop': 'off',
    'vue/html-closing-bracket-spacing': 'error',
  },
};

  1. 配置 typescript
//vue-shim.d.ts
declare module '*.vue' {
  import { Component, ComponentPublicInstance } from 'vue'
  const _default: Component & {
    new(): ComponentPublicInstance<any>
  }
  export default _default
}
//source.d.ts
import Vue from 'vue'
declare module '*.vue' {
  export default Vue
}

declare module '*.json'
declare module '*.png'
declare module '*.jpg'


仿element-ui的dialog组件

  • css直接使用elemnt的即可,自行去下载主题,并引入
  • dialog组件需要2个组件(全局的mask组件,dialog组件)

//dialog.ts

//dialog抽离的逻辑

// 全局引用

import Dialog from './components/dialog/index'
const app = createApp(App)
app.component(Dialog.name, Dialog)

// mask蒙层组件

//使用方式 v-model.modelValue(原:visible.sync="dialogVisible"上一篇有介绍)

<elDialog
    v-model.modelValue="dialogVisible" 
    title="提示"
    width="30vw"
    @close="handleClose">
    <p>这是一段信息~~~</p>
    <div slot="footer" class="dialog-footer">
      <button @click="handleClose" class="el-button el-button--default">取 消</button>
      <button type="primary" class="el-button el-button--primary" @click="dialogVisible = false">确 定</button>
    </div>
  </elDialog>

总结

至此,使用vue3.0+typescript开发dialog组件后,对vue3.0的开发有了进一步的了解。这个过程中,需要注意安装插件的版本。如果报错了,估计就是版本安装不对。下一篇就开始介绍router了~希望大家喜欢~ 代码github地址