前端开发tips

vue配置启动项目自动打开浏览器

打开package.json找到启动命令npm run dev 跟npm run serve(这两种命令都可以) 后面增加 --open

Vue项目设置路径src目录别名为@

  1. Vue2

编辑vue.config.js内容如下:

bash 复制代码
const { defineConfig } = require('@vue/cli-service')
 
const path = require('path')
function resolve(dir) {
  return path.join(__dirname, dir)
}
 
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave:false,// 关闭Eslint语法检查
  configureWebpack: {
    resolve: {
      alias: {
        '@': resolve('src'),
      },
    },
  }, 
})
  1. Vue3

编辑vite.config.ts内容如下:

bash 复制代码
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
export default defineConfig({
    plugins: [vue()],
    resolve: {
        alias: {
            "@": path.resolve("./src") // 相对路径别名配置,使用 @ 代替 src
        }
    }
})

编辑tsconfig.json内容如下:

bash 复制代码
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "./", // 解析非相对模块的基地址,默认是当前目录
    "paths": { //路径映射,相对于baseUrl
      "@/*": ["src/*"] 
    }
  }
}

Vue路由模块使用和封装模板

main.js

bash 复制代码
import Vue from 'vue'
import App from './App.vue'
import router from './router/index'

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

App.vue

bash 复制代码
<template>
  <div id="app">
    <!--路由页面-->
    <router-view/>
  </div>
</template>

<script>
export default {
    name: 'App'
  }
</script>

/router/index.js

bash 复制代码
import Vue from 'vue'
import VueRouter from 'vue-router'
// 插件初始化
Vue.use(VueRouter)
// 创建路由对象
const router=new VueRouter({
  routes:[
    {
      path:'/find',
      component: () => import('@/views/Find.vue'),
    },
    {
      path:'/friend',
      component: () => import('@/views/Friend.vue'),
    },
    {
      path:'/my',
      component: () => import('@/views/My.vue'),
    }
  ]
})

export default router
相关推荐
2301_768350231 天前
Vue第二期:组件及组件化和组件的生命周期
前端·javascript·vue.js
小周同学:1 天前
Vue项目中将界面转换为PDF并导出的实现方案
javascript·vue.js·pdf
华洛1 天前
公开一个AI产品的商业逻辑与设计方案——AI带来的涂色卡自由
前端·后端·产品
明远湖之鱼1 天前
opentype.js 使用与文字渲染
前端·svg·字体
90后的晨仔1 天前
Vue 3 组合式函数(Composables)全面解析:从原理到实战
前端·vue.js
今天头发还在吗1 天前
【React】动态SVG连接线实现:图片与按钮的可视化映射
前端·javascript·react.js·typescript·前端框架
小刘不知道叫啥1 天前
React 源码揭秘 | suspense 和 unwind流程
前端·javascript·react.js
szial1 天前
为什么 React 推荐 “不可变更新”:深入理解 React 的核心设计理念
前端·react.js·前端框架
mapbar_front1 天前
面试是一门学问
前端·面试
90后的晨仔1 天前
Vue 3 中 Provide / Inject 在异步时不起作用原因分析(二)?
前端·vue.js