当前位置:首页 > VUE

vue实现新闻导航栏

2026-01-21 20:49:08VUE

Vue实现新闻导航栏

使用Vue实现新闻导航栏可以通过组件化开发方式,结合路由和动态数据绑定完成。以下是具体实现方法:

基础结构搭建

创建Vue组件NewsNav.vue,包含导航栏HTML结构和基本样式:

<template>
  <div class="news-nav">
    <ul>
      <li v-for="(item, index) in navItems" :key="index">
        <router-link :to="item.path">{{ item.title }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navItems: [
        { title: '首页', path: '/' },
        { title: '国内', path: '/domestic' },
        { title: '国际', path: '/international' },
        { title: '科技', path: '/technology' },
        { title: '娱乐', path: '/entertainment' }
      ]
    }
  }
}
</script>

<style scoped>
.news-nav {
  background: #333;
  color: white;
}
.news-nav ul {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
}
.news-nav li {
  padding: 15px 20px;
}
.news-nav a {
  color: white;
  text-decoration: none;
}
.news-nav a:hover {
  color: #42b983;
}
</style>

动态高亮当前路由

添加路由激活状态样式,修改router-link为:

<router-link 
  :to="item.path" 
  active-class="active"
  exact-active-class="exact-active"
>
  {{ item.title }}
</router-link>

在样式中添加:

.news-nav .active {
  color: #42b983;
  border-bottom: 2px solid #42b983;
}

响应式设计

添加媒体查询实现移动端适配:

@media (max-width: 768px) {
  .news-nav ul {
    flex-direction: column;
  }
  .news-nav li {
    padding: 10px;
    text-align: center;
  }
}

数据动态加载

从API获取导航数据:

export default {
  data() {
    return {
      navItems: []
    }
  },
  async created() {
    try {
      const response = await fetch('/api/nav-items');
      this.navItems = await response.json();
    } catch (error) {
      console.error('加载导航数据失败:', error);
    }
  }
}

添加交互效果

实现下拉菜单功能:

<template>
  <div class="news-nav">
    <ul>
      <li v-for="(item, index) in navItems" :key="index" @mouseenter="showSubNav(index)" @mouseleave="hideSubNav(index)">
        <router-link :to="item.path">{{ item.title }}</router-link>
        <ul v-if="item.subItems && isSubNavVisible[index]" class="sub-nav">
          <li v-for="(subItem, subIndex) in item.subItems" :key="subIndex">
            <router-link :to="subItem.path">{{ subItem.title }}</router-link>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isSubNavVisible: []
    }
  },
  methods: {
    showSubNav(index) {
      this.$set(this.isSubNavVisible, index, true);
    },
    hideSubNav(index) {
      this.$set(this.isSubNavVisible, index, false);
    }
  }
}
</script>

<style>
.sub-nav {
  position: absolute;
  background: #444;
  padding: 0;
  margin-top: 15px;
}
.sub-nav li {
  padding: 10px 20px;
  white-space: nowrap;
}
</style>

与Vuex集成

使用Vuex管理导航状态:

// store/modules/nav.js
export default {
  state: {
    navItems: []
  },
  mutations: {
    SET_NAV_ITEMS(state, items) {
      state.navItems = items;
    }
  },
  actions: {
    async fetchNavItems({ commit }) {
      const response = await fetch('/api/nav-items');
      commit('SET_NAV_ITEMS', await response.json());
    }
  }
}

在组件中使用:

vue实现新闻导航栏

import { mapState } from 'vuex';

export default {
  computed: {
    ...mapState('nav', ['navItems'])
  },
  created() {
    this.$store.dispatch('nav/fetchNavItems');
  }
}

标签: 新闻vue
分享给朋友:

相关文章

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现多选

vue实现多选

Vue 实现多选功能 在 Vue 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组(…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue实现spa

vue实现spa

Vue 实现 SPA(单页应用)的步骤 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保安装了 Vue Router 作为路由管理工具。运行以下命令创建项目: vue c…