vue3使用vite搭建的项目需要安装的插件/配置方式

 更新时间:2024年03月09日 09:23:08   作者:sqwu  
这篇文章主要介绍了vue3使用vite搭建的项目需要安装的插件/配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

1.创建项目

项目名称是myProject

vue create myProject

2.vetur报错调整

由于vue3引入组件后不用在components中声明,vetur插件会报错

解决办法:vscode设置 -》 搜索vetur -》 找到下面这个选项,把他关掉。然后重新打开文件,发现没有报错了

3.vite2使用eslint校验

推荐使用这一篇的eslint配置方案👉【vite】vite项目从0开始配置eslint

不使用上一篇的话,就按下面的步骤来~

1.安装eslint

执行命令npx eslint --init,按照以下选项配置依赖,会顺带下载四个依赖

2.下面开始适配vue3

打开.eslinttrc.js

1.删除extends中的"plugin:vue/essential"

2.删除plugins中的"vue"

3.在extneds中添加'plugin:vue/vue3-recommended'

4.安装pnpm i -D prettier eslint-config-prettierpnpm i eslint-plugin-prettier

5.在extends里面加一句:'plugin:prettier/recommended'

4.使用vue-router

1.安装

npm install vue-router@4

2.在src下创建目录router/index.ts

注意引入方式import * as VueRouter from 'vue-router'。。

直接import VueRouter from 'vue-router'会报错

Uncaught SyntaxError: The requested module ‘/node_modules/.vite/deps/vue-router.js?v=8dd0ce81’ does not provide an export named ‘default’import * as VueRouter from ‘vue-router’

import * as VueRouter from 'vue-router'
const routes = [
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/About/index.vue')
  }
]
// 创建路由实例
const router = VueRouter.createRouter({
  history: VueRouter.createWebHashHistory(), // hash模式
  routes
})
export default router

3.在main.ts中应用router

import { createApp } from 'vue'
import App from './App.vue'

import router from './router'

const app = createApp(App)

// 使用路由
app.use(router)

app.mount('#app')

5.使用vuex

1.安装

npm install vuex@next --save

2.在src下创建目录

store
    ├── index.js          # 我们组装模块并导出 store 的地方
    ├── actions.js        # 根级别的 action
    ├── mutations.js      # 根级别的 mutation
    └── modules
        ├── cart.js       # 购物车模块
        └── products.js   # 产品模块

6.使用vant4

1.安装

npm i vant

2.安装插件实现按需引入组件的样式

// 1.安装插件依赖
npm i vite-plugin-style-import@1.4.1 -D

// 2.在vite.config.ts使用插件
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport, { VantResolve } from 'vite-plugin-style-import'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    styleImport({
      resolves: [VantResolve()]
    })
  ]
})

// 3.在页面中使用组件
<template>
    <van-button>登录</van-button>
</template>
<script lang="ts" setup>
import { Button as VanButton } from 'vant'
</script>

7.使用less

1.安装

npm install  less less-loader -d

8.移动端将px转为rem

1.安装插件

// 是postcss的插件,用于将像素单元生成rem单位。
npm install postcss-pxtorem -D
// 配置可伸缩布局方案,主要是将1rem设为viewWidth/10
npm install amfe-flexible -S

2.在main.ts中

import 'amfe-flexible'

3.在根目录下创建postcss.config.js

// eslint-disable-next-line no-undef
module.exports = {
  plugins: {
    autoprefixer: {
      overrideBrowserslist: [
        'Android 4.1',
        'iOS 7.1',
        'Chrome > 31',
        'ff > 31',
        'ie >= 8',
        'last 10 versions'
      ],
      grid: true
    },
    // 把px单位换算成rem单位
    'postcss-pxtorem': {
      rootValue: 75, // 换算的基数(设计图750的根字体为75)
      propList: ['*'], // *代表将项目中的全部进行转换,单个转换如width、height等
      unitPrecision: 5
    }
  }

9.使用axios

1.安装

npm install axios -S
npm install qs  -S

2.在utils目录下创建http.js,根据项目要求配置请求/响应拦截器

import Axios, { AxiosRequestConfig } from 'axios'
import { Toast } from 'vant'

// 设置请求头
Axios.defaults.headers.post['Content-Type'] = 'application/json'
// 设置超时
Axios.defaults.timeout = 6 * 1000

export default function $http({ url, method, params }: AxiosRequestConfig) {
  return new Promise((resolve, reject) => {
    const _axiosConfig = {
      url,
      method,
      params
    }
    Axios(_axiosConfig)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

3.在src目录下创建api目录,用来放接口请求

// api/common.ts
import $http from '../utils/http'
const url = 'https://dev.ylzpay.com/api/follow-up/app/api'
export function getDictInfo(params: unknown) {
  return $http({
    url,
    params
  })
}

// api/index.ts
export * from './common'

10.配置请求代理

// 在vite.config.ts
server: {
    port: 8077,
    open: true, // 自动打开
    base: './',
    proxy: {
      '/app/api': {
        target: 'https://dev.ylzpay.com/api/follow-up/app/api',
        changeOrigin: true, // 打开代理
        rewrite: path => path.replace('/app/api', '')
      }
    }
  }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

  • Vue+LogicFlow+Flowable实现工作流

    Vue+LogicFlow+Flowable实现工作流

    本文主要介绍了Vue+LogicFlow+Flowable实现工作流,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-12-12
  • vue3 中 computed 新用法示例小结

    vue3 中 computed 新用法示例小结

    这篇文章主要介绍 vue3 中 computed 的新用法,对比 vue2 中的写法,让您快速掌握 vue3 中 computed 的新用法,对函数式写法,options 写法相关知识感兴趣的朋友一起看看吧
    2021-11-11
  • vue中使用codemirror的实例详解

    vue中使用codemirror的实例详解

    这篇文章主要介绍了vue中使用codemirror的实例教程,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2018-11-11
  • vue中实现路由跳转的三种方式超详细教程

    vue中实现路由跳转的三种方式超详细教程

    这篇文章主要介绍了vue中实现路由跳转的三种方式超详细教程,其中声明式router-link实现跳转最简单的方法,可用组件router-link来替代a标签,每种方式给大家讲解的非常详细需要的朋友可以参考下
    2022-11-11
  • Vue实现页面刷新跳转到当前页面功能

    Vue实现页面刷新跳转到当前页面功能

    在Vue.js应用开发中,有时候我们需要实现页面的刷新或跳转到当前页面的功能,这种需求在某些特定场景下非常有用,本文将详细介绍如何在Vue中实现页面刷新和跳转到当前页面的功能,并提供多个示例和使用技巧,需要的朋友可以参考下
    2024-10-10
  • Vant picker选择器设置默认值导致选择器失效的解决

    Vant picker选择器设置默认值导致选择器失效的解决

    这篇文章主要介绍了Vant picker选择器设置默认值导致选择器失效的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • vue中的文本空格占位符说明

    vue中的文本空格占位符说明

    这篇文章主要介绍了vue中的文本空格占位符说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • vue-cli中的babel配置文件.babelrc实例详解

    vue-cli中的babel配置文件.babelrc实例详解

    Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行。本文介绍vue-cli脚手架工具根目录的babelrc配置文件,感兴趣的朋友一起看看吧
    2018-02-02
  • vue项目之index.html如何引入JS文件

    vue项目之index.html如何引入JS文件

    这篇文章主要介绍了vue项目之index.html如何引入JS文件问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • Vue3 源码导读(推荐)

    Vue3 源码导读(推荐)

    这篇文章主要介绍了Vue3 源码导读(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10

最新评论