Backend解析
约 355 字大约 2 分钟
在vite.config.js
,导入Element-UI-plus
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig({
plugins: [
vue(),
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
})
安装路由
npm install vue-router
index.js
import {createRouter, createWebHashHistory} from 'vue-router'
import {unauthorized} from "@/net";
const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'welcome',
component: () => import('@/views/WelcomeView.vue'),
children: [
{
path: '',
name: 'welcome-login',
component: () => import('@/views/welcome/LoginPage.vue')
},
{
path: 'register',
name: 'welcome-register',
component: () => import('@/views/welcome/RegisterPage.vue')
},
{
path: 'reset',
name: 'welcome-reset',
component: () => import('@/views/welcome/ResetPage.vue')
}
]
},
{
path: '/index',
name: 'index',
component: () => import('@/views/IndexView.vue')
}
]
})
router.beforeEach((to, from, next) => {
var isUnauthorized = unauthorized();
if (to.name.startsWith('welcome') && !isUnauthorized) {
next('/index');
} else if (to.fullPath.startsWith('/index') && isUnauthorized) {
next('/')
} else {
next();
}
})
export default router
main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import axios from "axios";
import 'element-plus/theme-chalk/dark/css-vars.css'
axios.defaults.baseURL = 'https://shenjianl.cn:4566'
const app = createApp(App)
app.use(router)
app.mount('#app')
安装element-ui
# 选择一个你喜欢的包管理器
# NPM
$ npm install element-plus --save
# Yarn
$ yarn add element-plus
# pnpm
$ pnpm install element-plus
按需导入
首先你需要安装unplugin-vue-components
和 unplugin-auto-import
这两款插件
npm install -D unplugin-vue-components unplugin-auto-import
// vite.config.ts
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig({
// ...
plugins: [
// ...
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
})
导入图标
import {User, Lock} from '@element-plus/icons-vue'
登录界面编写
axios封装
先安装axios
npm install axios
ElMessage无需导入
配置 baseurl
axios.defaults.baseURL = 'http://127.0.0.1:4566'