首页 > 编程语言 >2024年1月Java项目开发指南10:vite+Vue3项目创建

2024年1月Java项目开发指南10:vite+Vue3项目创建

时间:2024-01-25 22:25:48浏览次数:21  
标签:10 vue Java 2024 router import layout 路由 const

image

image

新建项目

image

image

安装router

npm install vue-router

image

在src下新建目录router,在目录下新建index.js

image

在index.js里面配置路由

import { createRouter, createWebHistory } from 'vue-router';

// 定义路由
const routes = [
  //在这里配置路由
];

// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes,
});

export default router;

然后编辑main文件

image


import { createApp } from 'vue'
import './style.css'
import router from "./router/index.js";
import App from './App.vue'
let app = createApp(App)
app.use(router).mount('#app')


安装AntDesignVue

官方文档:
https://www.antdv.com/docs/vue/getting-started-cn

npm i --save ant-design-vue

安装后再main.js中配置

import { createApp } from 'vue'
import './style.css'
import router from "./router/index.js";
import Antd from 'ant-design-vue';
import App from './App.vue'
import 'ant-design-vue/dist/reset.css';
let app = createApp(App)
app.use(router).use(Antd).mount('#app')

安装AntDesignVue的icon图标

npm install --save @ant-design/icons-vue

修改App.vue

<script setup>

</script>

<template style="width: 100%;height: 100%;margin: 0;padding: 0">
  <router-view style="width: 100%;height: 100%"></router-view>
</template>

<style scoped>

</style>

创建视图view

在src下创建views文件夹
在文件夹下创建ControlView.vue作为控制中心页面
同时配置路由

{  
	path: '/',
	name: 'ControlCenter',
	component: () => import('../views/ControlView.vue')
}

router.index.js文件配置如下

import { createRouter, createWebHistory } from 'vue-router';

// 定义路由
const routes = [
    {
        path: '/',
        name: 'ControlCenter',
        component: () => import('../views/ControlView.vue'),

    },
    // 其他路由...
];

// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes,
});

export default router;

修改index.html

image

删除style.css里面#app的样式

image

你可以尝试在 ControlView.vue这个文件写代码,然后运行看看效果

image

我这个文件的代码贴出来你们参考一下

<template style="width: 100%">
  <div class="flex-layout" style="display: flex; flex-direction: column; min-height: 100vh;">
  <a-layout class="layout" style="width: 100%;flex:1">
    <a-layout-header style="width: 100%;margin: 0;padding: 0">
      <a-menu
          v-model:selectedKeys="selectedKeys"
          theme="dark"
          mode="horizontal"
          :style="{ lineHeight: '64px',display:'flex', justifyContent: 'space-between' }"
          class="menu-custom"
      >

        <a-menu-item key="1"><router-link to="/index">首页</router-link></a-menu-item>
          <a-menu-item key="2"><router-link to="/index">关于</router-link></a-menu-item>
        <a-menu-item key="3"><router-link to="/admin">后台</router-link></a-menu-item>

          <a-menu-item v-if="isLoggedIn" style="" key="4" disabled>
            <a-avatar style="color: #f56a00; background-color: #fde3cf">{{ user.nickname.charAt(0) }}</a-avatar>
            <span style="margin-left: 8px;">{{ user.nickname }}</span>
            <a-divider type="vertical"></a-divider>
            <a-button type="primary" danger @click="handleLogout">退出登录</a-button>
          </a-menu-item>
          <a-menu-item v-else key="5" disabled>
            <a href="/login">登录</a>
          </a-menu-item>


      </a-menu>
    </a-layout-header>

    <a-layout-content style="padding: 0 50px;overflow-y: auto">
      <router-view></router-view>
    </a-layout-content>

    <a-layout-footer class="footer" style="text-align: center; background-color: #f0f0f0; padding: 16px 0;">
      千杯烈酒求一醉 王的孤独谁体会
    </a-layout-footer>
  </a-layout>
  </div>
</template>
<script setup>
import {onMounted, ref, watchEffect} from 'vue';
import {useRoute} from "vue-router";


const selectedKeys = ref(['1']);
const route = useRoute();
// 使用 watchEffect 来监听路由变化
watchEffect(() => {
  // 根据路由路径更新 selectedKeys
  switch (route.path) {
    case '/index':
      selectedKeys.value = ['1'];
      break;
    case '/about':
      selectedKeys.value = ['2'];
      break;
    case '/admin':
      selectedKeys.value = ['3'];
      break;
    default:
      selectedKeys.value = ['1']; // 默认选中的 key
      break;
  }
});


const isLoggedIn = ref(false);
const user = ref({
  avatar: '',
  nickname: localStorage.getItem("username")
});

onMounted(async () => {

});

function handleLogout() {
  // 退出登录逻辑
  localStorage.removeItem('token');
  isLoggedIn.value = false;
  user.value = {avatar: '', nickname: ''};
}
</script>
<style scoped>
.site-layout-content {
  min-height: 280px;
  padding: 24px;
  background: #fff;
}

#components-layout-demo-top .logo {
  float: left;
  width: 120px;
  height: 31px;
  margin: 16px 24px 16px 0;
  background: rgba(255, 255, 255, 0.3);
}

.ant-row-rtl #components-layout-demo-top .logo {
  float: right;
  margin: 16px 0 16px 24px;
}

[data-theme='dark'] .site-layout-content {
  background: #141414;
}

.menu-custom{
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.flex-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh; /* 确保容器至少占据整个视口高度 */
}

.layout {
  flex: 1; /* 让布局占据所有可用空间 */
  display: flex;
  flex-direction: column;
}

.footer {
  /* 页脚样式,根据需要自定义 */
  margin-top: auto; /* 将页脚推到容器的底部 */
}
</style>

我不建议你抄我代码。
请自行查阅AntDesignVue官方文件,自己设计界面

https://www.antdv.com/components/layout-cn

标签:10,vue,Java,2024,router,import,layout,路由,const
From: https://www.cnblogs.com/mllt/p/17988278/project202401-10

相关文章

  • java中的ThreadLocal
    1.ThreadLocal的基本使用在Java的多线程并发执行过程中,为了保证多个线程对变量的安全访问,可以将变量放到ThreadLocal类型的对象中,使变量在每个线程中都有独立值,不会出现一个线程读取变量时而被另一个线程修改的现象。ThreadLocal类通常被翻译为线程本地变量类或者线程局部变......
  • Java方法详解
    Java方法详解1、何谓方法System.out.println(),那么它是什么呢?Java方法是语句的集合,他们在一起执行以一个功能。方法是解决一类问题的步骤的有序组合方法包含于类或对象中方法的程序中被创建,在其他地方被引用设计方法的原则:方法的本意是功能块,就是实现某个功......
  • 2024转行程序员的请注意:均月薪在40-70k
    前言2023年,对大多数行业来说都是不太好过的一年。对程序员来说也是如此,很多粉丝朋友都在说android工作特别难找,一个岗位都是几千份简历........大家心里都是特别的焦虑,本以为2024年就业情况会有好转,但实际上并非如此。没想到2024年的开启,却是硅谷大裁员!2024年才过了十几天,就传......
  • 使用 JavaScript 宏删除文档中的特定注释
    有时只需要删除文档中的注释,要怎么快速做到呢?在这篇文章中,我们将会展示如何为ONLYOFFICE创建一个简单的宏,来删除某些特定的或所有评论,从而保持协作的重点和整洁。什么是ONLYOFFICE 宏如果您是一名资深MicrosoftExcel用户,那么相信您已对于VBA宏非常熟悉了。这些宏是帮助您自......
  • 了解Java事务管理
    在软件开发过程中,事务管理是一个非常重要的概念.事务用于确保数据库操作的一致性和完整性,并且具有原子性、一致性、隔离性和持久性的特性.Java提供了强大的事务管理机制,使得我们能够更好地处理数据的一致性和并发控制.Java事务管理主要通过JavaTransactionAPI(JTA)和Java......
  • winter 2024 day2
    2023中国大学生程序设计竞赛(CCPC)新疆赛区(重现赛H数学思路:有四平方和定理知道,任意正整数可表示为不超过四个整数的平方和。 并且n的范围为1e5,可以枚举出f(x)值为1、2、3、4的平方数组合情况。也可以dp,f[i]=min(f[i],f[i-k*k]+1)#include<bits/stdc++.h>usingnamespacestd......
  • Java学习日记 Day11
    Maven:把maven课程速通了,比较简单,其实就是对工程框架的一个配置,可以用一个总pom文件让整个工程的版本得到确定。SpringMVC:是Servlet的plus版,今天开了个头,明天继续学。算法:①二叉树的所有路径:递归加回溯,用一个List储存结果,一个双向队列储存路径。如果没遇到叶子节点就继续向里递......
  • java初学者
    day2packagebase;publicclassTest05{publicstaticvoidmain(String[]args){inti=128;byteb=(byte)i;//强制转换(类型)变量名System.out.println(i);System.out.println(b);bytea=12;intc......
  • 2024/1/25学习进度笔记
    平方误差代价函数线性回归有一个训练集,我们选择了线性回归,那么要如何选择合适的参量使得我们的预测更为准确呢??我们知道了现有的数据是准确的,那么预测就要以现有的数据为根基,尽量的贴合现有的数据,使得差距最小,怎么衡量这个差距呢???我们把  平均平方和误差为了方便,我们又......
  • 每日一题 2024-1-25
    1.题目(1278)原题链接给你一个下标从0开始的整数数组\(nums\)和一个整数\(k\)。请你用整数形式返回\(nums\)中的特定元素之和,这些特定元素满足:其对应下标的二进制表示中恰存在\(k\)个置位。整数的二进制表示中的1就是这个整数的置位。例如,\(21\)的二进制表示为......