当前位置:首页 > 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;
}

响应式设计

vue实现新闻导航栏

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

@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);
    }
  }
}

添加交互效果

vue实现新闻导航栏

实现下拉菜单功能:

<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());
    }
  }
}

在组件中使用:

import { mapState } from 'vuex';

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

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

相关文章

vue如何实现冒泡

vue如何实现冒泡

Vue 实现冒泡排序 在 Vue 中实现冒泡排序,可以通过数据绑定和响应式特性动态展示排序过程。以下是一个完整的实现示例,包含代码和详细说明。 数据准备 在 Vue 的 data 中定义一个数组作…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签…

vue computed 实现

vue computed 实现

computed 的基本用法 在 Vue 中,computed 用于声明计算属性,这些属性基于响应式依赖进行缓存。只有当依赖发生变化时,计算属性才会重新计算。 new Vue({ data:…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于…