shanshandeisu 2024-02-08 17:55 采纳率: 58.3%
浏览 29
已结题

一个vue3项目的页面跳转失效问题

前言

本人是后端,只会前端三件套,不会vue。但是有个紧急任务就是把原本的一个三件套写的项目转为vue3。所以我用vue-cli创建了一个vue3项目。项目打开的首页是这样的

在这里插入图片描述


它导航栏的"合作案例"“服务团队”“联系我们”分别对应着Services.vue、Team.vue、Contact.vue。但是现在没办法点击并跳转。

项目整体结构

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/18a1d42254d048f394259b839674653a.png =400x)

App.vue

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

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

Navbar.vue

<template>
    <div class="navbar">
        <div class="logo"><a href="#">深圳市淮亚之光科技创新有限公司</a></div>
        <ul class="links">
            <li><router-link to="/">首页</router-link></li>
            <li><router-link to="/services">合作案例</router-link></li>
            <li><router-link to="/team">服务团队</router-link></li>
            <li><router-link to="/contact">联系我们</router-link></li>
        </ul>
        <a href="#" class="action_btn">中文/English</a>
        <div class="toggle_btn">
            <i class="fa-solid fa-bars"></i>
        </div>
    </div>

    <div class="dropdown_menu" :class="{ open: DropdownMenuOpen }">
        <li><router-link to="/">首页</router-link></li>
        <li><router-link to="/services">合作案例</router-link></li>
        <li><router-link to="/team">服务团队</router-link></li>
        <li><router-link to="/contact">联系我们</router-link></li>
        <li><a href="#" class="action_btn">中文/English</a></li>
    </div>
</template>

<script>
export default {
    name: "Navbar",
    data() {
        return {
            DropdownMenuOpen: false
        };
    },
    methods: {
        handleWindowsResize() {
            if (window.innerHeight > 768) {
                this.DropdownMenuOpen = false;
            }
        }
    },
    mounted() {
        window.addEventListener("resize", this.handleWindowsResize);
        this.handleWindowsResize();
    },
    beforeDestroy() {
        window.removeEventListener("resize", this.handleWindowsResize);
    }
};
</script>

<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Open Sans', sans-serif;
}

li {
    list-style: none;
}

a {
    text-decoration: none;
    color: #fff;
    font-size: 1rem;
}

a:hover {
    color: orange;
}

/* 导航栏 */
header {
    position: relative;
    padding: 0 2rem;
}

.navbar {
    width: 100%;
    height: 60px;
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.navbar .logo a {
    font-size: 1.5rem;
    font-weight: bold;
}

.navbar .links {
    display: flex;
    gap: 2rem;
}

.navbar .toggle_btn {
    color: #fff;
    font-size: 1.5rem;
    cursor: pointer;
    display: none;
}



.action_btn {
    background-color: orange;
    color: #fff;
    padding: 0.5rem 1rem;
    border: none;
    outline: none;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: bold;
    cursor: pointer;
    transition: scale 0.2 ease;
}

.action_btn:hover {
    scale: 1.05;
    color: #fff;
}

.action_btn:active {
    scale: 0.95;
}




/* 下拉式菜单 */
.dropdown_menu {
    display: none;
    position: absolute;
    right: 2rem;
    top: 60px;
    height: 0;
    width: 300px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(15px);
    border-radius: 10px;
    overflow: hidden;
    transition: height 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.dropdown_menu.open {
    height: 240px;
}


.dropdown_menu li {
    padding: 0.7rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dropdown_menu .action_btn {
    width: 100%;
    display: flex;
    justify-content: center;
}
</style>

Index.vue

<template>
  <header>
    <Navbar></Navbar>
  </header>
  <main>
    <section id="index">
      <h1>助力中小企业数字化转型</h1>
      <p>品质卓越 共赢至上</p>
    </section>
  </main>
</template>

<script>
import Navbar from '@/components/Navbar.vue';

export default {
  name: 'index',
  components: {
    Navbar
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Open Sans', sans-serif;
}

body {
  height: 100vh;
  background-color: #000;
  background-image: url('https://images.unsplash.com/photo-1485470733090-0aae1788d5af?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1517&q=80');
  background-size: cover;
  background-position: center;
}

li {
  list-style: none;
}

a {
  text-decoration: none;
  color: #fff;
  font-size: 1rem;
}

a:hover {
  color: orange;
}

/* 导航栏 */
header {
  position: relative;
  padding: 0 2rem;
}

.navbar {
  width: 100%;
  height: 60px;
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.navbar .logo a {
  font-size: 1.5rem;
  font-weight: bold;
}

.navbar .links {
  display: flex;
  gap: 2rem;
}

.navbar .toggle_btn {
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
  display: none;
}

.action_btn {
  background-color: orange;
  color: #fff;
  padding: 0.5rem 1rem;
  border: none;
  outline: none;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: bold;
  cursor: pointer;
  transition: scale 0.2 ease;
}

.action_btn:hover {
  scale: 1.05;
  color: #fff;
}

.action_btn:active {
  scale: 0.95;
}

/* 下拉式菜单 */
.dropdown_menu {
  display: none;
  position: absolute;
  right: 2rem;
  top: 60px;
  height: 0;
  width: 300px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(15px);
  border-radius: 10px;
  overflow: hidden;
  transition: height 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.dropdown_menu.open {
  height: 240px;
}


.dropdown_menu li {
  padding: 0.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dropdown_menu .action_btn {
  width: 100%;
  display: flex;
  justify-content: center;
}

/* 首页正文 */
section#index {
  height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #fff;
}

#index h1 {
  font-size: 3rem;
  margin-bottom: 1rem;
}

/* 响应式设计 */
@media(max-width: 992px) {
  .navbar .links,
  .navbar .action_btn {
    display: none;
  }

  .navbar .toggle_btn {
    display: block;
  }

  .dropdown_menu {
    display: block;
  }
}

@media(max-width: 576px) {
  .dropdown_menu {
    left: 2rem;
    width: unset;
  }
}
</style>

Team.vue

<template>
    <header>
        <div include-html="navbar_eng.html"></div>
    </header>
    <div class="shell">
        <div class="content">
            <div class="item"></div>
            <div class="item"></div>
            <div class="item"></div>
        </div>
    </div>
</template>

<script>
import Navbar from '@/components/Navbar.vue';
export default {
    name: 'team',
    components: {
        Navbar
    }
}
</script>

<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Open Sans', sans-serif;
}

body {
    /* display: flex; */
    align-items: center;
    height: 100vh;
    background-color: #000;
    background-image: url('https://images.unsplash.com/photo-1485470733090-0aae1788d5af?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1517&q=80');
    background-size: cover;
    background-position: center;
}


.shell {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100vw;
    height: 200vh;
    max-width: 484px;
    max-height: 422px;
    margin: auto;
    color: white;
    perspective: 1000px;
    transform-origin: center;
    display: flex;
    justify-content: center;
    align-items: center;
}


.content {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    margin: auto;
    transform-origin: center;
    transform-style: preserve-3d;
    transform: translateZ(-30vw) rotateY(0);
    animation: carousel 9s infinite cubic-bezier(0.77, 0, 0.175, 1) forwards;

}

.item {
    position: absolute;
    width: 60vw;
    height: 40vw;
    max-width: 380px;
    max-height: 250px;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
    border-radius: 6px;
    background-size: cover;
    -webkit-box-reflect: below 25px -webkit-linear-gradient(transparent 50%, rgba(255, 255, 255, 0.3));
}

.item:nth-child(1) {
    background-image: url("../assets/1.jpg");
    transform: rotateY(0) translateZ(35vw);
}

.item:nth-child(2) {
    background-image: url("../assets/4.jpg");
    transform: rotateY(120deg) translateZ(35vw);
}


.item:nth-child(3) {
    background-image: url("../assets/3.jpg");
    transform: rotateY(240deg) translateZ(35vw);
}

@keyframes carousel {

    0%,
    17.5% {
        transform: translateZ(-35vw) rotateY(0);
    }

    27.5%,
    45% {
        transform: translateZ(-35vw) rotateY(-120deg);
    }

    55%,
    72.5% {
        transform: translateZ(-35vw) rotateY(-240deg);
    }

    82.5%,
    100% {
        transform: translateZ(-35vw) rotateY(-360deg);
    }
}
</style>

router包下面的index.js

import { createRouter, createWebHistory } from 'vue-router'
import Index from "../views/Index.vue"
import Contact from '../views/Contact.vue';
import Services from '../views/Services.vue';
import Team from '../views/Team.vue'
const routerHistory = createWebHistory()

const router = createRouter({
    history: routerHistory,
    routes: [
        {
            path: '/',
            name: 'index',
            component: Index
        },
        {
            path: '/contact',
            name: 'contact',
            component: Contact
        },
        {
            path: '/services',
            name: 'services',
            component: Services
        },
        {
            path: '/team',
            name: 'team',
            component: Team
        }
    ]
}
)

想问下前端同学们,该怎么解决这个问题呢?

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-02-08 17:57
    关注

    引自免费微信小程序:皆我百晓生

    根据您提供的代码片段,您在Vue3项目中创建了一个基本的路由系统,并且页面结构和样式都已经配置好了。从您的描述来看,问题是点击导航栏中的router-link时,页面并没有正确跳转到对应的视图组件。为了帮助您解决这个问题,请按照以下步骤检查和调试:

    1. 确保路由配置正确: 检查src/router/index.js或相应位置的路由配置文件,确认路径、名称与对应的组件是否一一对应,看起来您提供的路由配置是正确的。
    import { createRouter, createWebHistory } from 'vue-router'
    import Index from "../views/Index.vue"
    import Contact from '../views/Contact.vue';
    import Services from '../views/Services.vue';
    import Team from '../views/Team.vue'
    
    const routerHistory = createWebHistory()
    
    const router = createRouter({
      history: routerHistory,
      routes: [
        {
          path: '/',
          name: 'index',
          component: Index
        },
        {
          path: '/contact',
          name: 'contact',
          component: Contact
        },
        {
          path: '/services',
          name: 'services',
          component: Services
        },
        {
          path: '/team',
          name: 'team',
          component: Team
        }
      ]
    })
    
    export default router
    
    1. 确认App.vue中已使用路由器: 根据您给出的App.vue代码,您已经正确地挂载了路由器。
    import { createApp } from 'vue'
    import App from './App.vue'
    import router from './router'
    // ... 其他导入
    
    createApp(App).use(router).mount('#app')
    
    1. 检查Navbar组件中的router-link: 确保router-linkto属性指向的是有效的路由路径。在您提供的Navbar组件代码中,路由链接看上去也是正确的。

    2. 查看视图组件是否加载: 确认Index.vueContact.vueServices.vueTeam.vue这些视图组件已经被正确地导入并且它们的内容是完整的Vue单文件组件格式。

    3. 查看浏览器控制台错误: 在浏览器开发者工具(例如Chrome DevTools)中查看控制台是否有任何JavaScript错误,这可能是导致路由无法正常工作的原因。

    如果以上所有内容都检查无误,而问题依然存在,请尝试重启开发服务器或者清除浏览器缓存,有时候缓存可能导致路由更新未生效。另外,也可以尝试手动输入地址进行跳转测试,以排除router-link渲染或事件绑定的问题。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月8日
  • 创建了问题 2月8日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀