前端开发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
相关推荐
happy_0x3f5 分钟前
前端应用的离线暂停更新策略
开发语言·前端·php
m0_5474866620 分钟前
《Vue.js + uni-app全栈开发从入门到实践 》全套PPT课件2026版
vue.js·uni-app
你驴我42 分钟前
WhatsApp 消息撤回与编辑的幂等性设计实践
java·服务器·前端·后端·python
新中地GIS开发老师1 小时前
零基础WebGIS开发入门 | GeoJSON数据持久化
前端·javascript·gis·webgis·三维gis开发
sunywz2 小时前
【c#】 Web Deploy一键发布,IIS部署全流程
开发语言·前端·c#
宁&沉沦2 小时前
Chrome 扩展 Manifest 字段版本支持一览(全量)
前端·后端·编辑器
乖孩子2 小时前
Service Worker + IndexedDB 实践
前端
浮生望2 小时前
BFF架构实战:用Express+SSE为Vue3搭建安全的流式对话中间层
前端
达子6662 小时前
第10章_HarmonyOs开发图解 用JS开发UI
vue.js
先吃饱再说3 小时前
React 组件通信:从 Props 单向传递到自定义事件
前端·react.js·前端框架