vue 登录

1.创建项目





bash 复制代码
Set-ExecutionPolicy RemoteSigned
npm install -g yarn
yarn add axios
yarn add element-plus

package.json

java 复制代码
{
  "name": "tom6",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "axios": "^1.7.2",
    "core-js": "^3.8.3",
    "element-plus": "^2.7.5",
    "vue": "^3.2.13",
    "vue-router": "^4.0.3",
    "vuex": "^4.0.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-router": "~5.0.0",
    "@vue/cli-plugin-vuex": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "sass": "^1.32.7",
    "sass-loader": "^12.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead",
    "not ie 11"
  ]
}

router/index.js

javascript 复制代码
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue';

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

main.js

javascript 复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

createApp(App).use(store).use(ElementPlus).use(router).mount('#app')

LoginView.vue

html 复制代码
<template>
    <div class="login">
      <h1>登录</h1>
      <el-form>
        <el-form-item label="用户名">
          <el-input v-model="username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  import { ElNotification } from 'element-plus';
  
  export default {
    name: 'LoginView',
    data() {
      return {
        username: '',
        password: ''
      }
    },
    methods: {
      async login() {
        try {
          const response = await axios.post('/api/login', {
            username: this.username,
            password: this.password
          });
          if (response.data.success) {
            // Handle login success
            ElNotification({
              title: 'Success',
              message: '登录成功',
              type: 'success',
            });
            localStorage.setItem('token', response.data.token);
            this.$router.push({ name: 'home' }); // Redirect to home page
          } else {
            // Handle login failure
            ElNotification({
              title: 'Error',
              message: '登录失败',
              type: 'error',
            });
          }
        } catch (error) {
          ElNotification({
            title: 'Error',
            message: `登录过程中发生错误: ${error.message}`,
            type: 'error',
          });
        }
      }
    }
  }
  </script>
  
  <style scoped>
  .login {
    max-width: 300px;
    margin: 0 auto;
    padding: 20px;
  }
  </style>

vue.config.js

javascript 复制代码
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
})


// vue.config.js
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:8181',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  }
};
相关推荐
ZengLiangYi7 分钟前
Fastify 加 Electron:把 Web 服务嵌进桌面应用
前端·javascript·后端
qq_25183645727 分钟前
基于nodejs express +vue 天天商城系统设计与实现 (源码 文档)
前端·vue.js·express
胡萝卜术1 小时前
从零搭建生成式AI项目:OpenAI + Node.js 环境配置与密钥安全实践
前端·javascript·面试
柒和远方1 小时前
每日一学V012: 从 Python 到 Node.js:一个 AI Native 开发者的 JavaScript 调用 LLM 实战
javascript·node.js·api
lichenyang4531 小时前
鸿蒙实战:聊天记录持久化 · 历史会话页面 · 两个真实 Bug 的定位与修复
前端
STDD1 小时前
Farming Simulator 25(模拟农场 25) Linux 专服搭建完全指南
linux·运维·javascript
天蓝色的鱼鱼1 小时前
前端也能写 AI Agent?用 Vercel AI SDK 十分钟跑通你的第一个智能助手
前端·ai编程
DevUI团队1 小时前
接口即代码:一个Skill轻松搞定类型定义、接口调用、Mock与调试
前端·agent·ai编程
DevUI团队1 小时前
从截图到企业级前端页面:2个Skill,1次对话,10X效率开发符合设计/编码规范的页面
前端·agent·ai编程