VUE JS 中的路由器 Link 未显示为活动
Router Link in VUE JS is not showing active
希望您度过了愉快的一周。问题是我现在正在学习 VUE 几个星期,但目前我正面临 VUE Router 的问题。以下是我面临的问题:
1- 我已经注册了5条路线并且已经创建了相应的视图。我创建了一个单独的文件调用“Navbar.vue”,我在其中使用此路由-link 作为主导航菜单。以下是我创建的路线 links:
- 首页
- 关于
- 电影
- 演员
- 简介
下面附上输出。
- 现在的问题是每个 link 工作正常,除了“电影”,我无法将鼠标悬停在它上面。但是每当我删除副官 link 时,都会出现同样的问题。以下是路由器的代码 link:
<div class="navLink col-3">
<router-link class="link" to="/">Home</router-link>
<router-link class="link" to="/about">About</router-link>
<router-link class="link" to="/movies">Movies</router-link>
<router-link class="link" to="/actors">Actors</router-link>
<router-link class="link" to="/profile">Profile</router-link>
</div>
路线注册
const routes = [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/about',
name: 'About',
component:About,
},
{
path: '/actors',
name: 'Actors',
component: Actors,
},
{
path: '/movies',
name: 'Movies',
component: Movies,
},
{
path: '/profile',
name: 'Profile',
component: Profile,
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
HTML 控制台输出
谁能指导我到底缺少什么? PS创建每条路由对应的视图link,每条路由注册成功
谢谢
第 1 步: HTML
模板
<template>
<div id="app">
<div class="navLink col-3">
<router-link class="link" to="/">Home</router-link>
<router-link class="link" to="/about">About</router-link>
<router-link class="link" to="/movies">Movies</router-link>
<router-link class="link" to="/actors">Actors</router-link>
<router-link class="link" to="/profile">Profile</router-link>
</div>
<router-view></router-view>
</div>
</template>
步骤 2: 创建 router
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../components/Home.vue";
import About from "../components/About.vue";
import Actors from "../components/Actors.vue";
import Movies from "../components/Movies.vue";
import Profile from "../components/Profile.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "Home",
component: Home
},
{
path: "/about",
name: "About",
component: About
},
{
path: "/actors",
name: "Actors",
component: Actors
},
{
path: "/movies",
name: "Movies",
component: Movies
},
{
path: "/profile",
name: "Profile",
component: Profile
}
];
const router = new VueRouter({
mode: "history",
linkExactActiveClass: "active",
routes
});
export default router;
步骤 3: 添加 style
以激活 class
<style>
.link {
margin: 10px;
}
.link.active {
background-color: red;
color: white;
}
</style>
希望您度过了愉快的一周。问题是我现在正在学习 VUE 几个星期,但目前我正面临 VUE Router 的问题。以下是我面临的问题:
1- 我已经注册了5条路线并且已经创建了相应的视图。我创建了一个单独的文件调用“Navbar.vue”,我在其中使用此路由-link 作为主导航菜单。以下是我创建的路线 links:
- 首页
- 关于
- 电影
- 演员
- 简介
下面附上输出。
- 现在的问题是每个 link 工作正常,除了“电影”,我无法将鼠标悬停在它上面。但是每当我删除副官 link 时,都会出现同样的问题。以下是路由器的代码 link:
<div class="navLink col-3">
<router-link class="link" to="/">Home</router-link>
<router-link class="link" to="/about">About</router-link>
<router-link class="link" to="/movies">Movies</router-link>
<router-link class="link" to="/actors">Actors</router-link>
<router-link class="link" to="/profile">Profile</router-link>
</div>
路线注册
const routes = [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/about',
name: 'About',
component:About,
},
{
path: '/actors',
name: 'Actors',
component: Actors,
},
{
path: '/movies',
name: 'Movies',
component: Movies,
},
{
path: '/profile',
name: 'Profile',
component: Profile,
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
HTML 控制台输出
谢谢
第 1 步: HTML
模板
<template>
<div id="app">
<div class="navLink col-3">
<router-link class="link" to="/">Home</router-link>
<router-link class="link" to="/about">About</router-link>
<router-link class="link" to="/movies">Movies</router-link>
<router-link class="link" to="/actors">Actors</router-link>
<router-link class="link" to="/profile">Profile</router-link>
</div>
<router-view></router-view>
</div>
</template>
步骤 2: 创建 router
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../components/Home.vue";
import About from "../components/About.vue";
import Actors from "../components/Actors.vue";
import Movies from "../components/Movies.vue";
import Profile from "../components/Profile.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "Home",
component: Home
},
{
path: "/about",
name: "About",
component: About
},
{
path: "/actors",
name: "Actors",
component: Actors
},
{
path: "/movies",
name: "Movies",
component: Movies
},
{
path: "/profile",
name: "Profile",
component: Profile
}
];
const router = new VueRouter({
mode: "history",
linkExactActiveClass: "active",
routes
});
export default router;
步骤 3: 添加 style
以激活 class
<style>
.link {
margin: 10px;
}
.link.active {
background-color: red;
color: white;
}
</style>