vue中tab栏切换

博客介绍了Vue中绑定类名的方法,即 :class 绑定布尔类型属性。还给出了CSS样式示例。同时分享了项目中Tab栏切换文章详情的代码,包括Tab栏代码、文章详情页对应、引入接口、主要方法等,以及详情页跳转和显示的相关内容,还提到了CSS样式引入问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.绑定类名 :class="{'属性名:后面为布尔类型,true为生效'}"

 

2.定义

 

3)在css中样式

 .active {

      font-size: 18px;

      color: #2878ff;

    }

 

扩展知识,在我做的项目中:关于tab栏切换文章详情代码

1.tab栏代码

 <div class="menu">
        <!-- 比较成熟的tab栏切换 -->
        <ul>
          <li :class="{ active: isActive === -1 }" @click="changeClass(-1)">
            推荐
          </li>
          <li
            :class="{ active: isActive == index }"
            v-for="(item, index) in artList"
            :key="index"
            @click="changeClass(index, item)"
          >
            {{ item.className }}
          </li>
        </ul>
      </div>

2.文章详情页对应

  <div class="artical">
      <el-card class="item" v-for="(item, index) in articalList" :key="index">
        <div class="title">
          <el-avatar size="small" :src="item.avatar"></el-avatar>
          <span>{{ item.username }}</span>
          <span>{{ item.updateTime | formatTime }}</span>
          <h5>
            <router-link :to="'/detail/' + item.articalId">{{
              item.articalName
            }}</router-link>
          </h5>
        </div>

3.引入方法接口 定义变量

4.主要方法 

  created() {
    this.initClassify();
    this.initArtical();
  },
  methods: {
     changeClass(index, item) {
      this.currentPage = 1;
      this.isActive = index;
      if (item) {
        this.classId = item.classId;
      } else {
        this.classId = null;
      }
      this.initArtical();
    },
    initClassify() {
      getAllClassify().then((res) => {
        if (res.code == 200) {
          // console.log(res);
          this.artList = res.data;
        }
      });
    },
    initArtical() {
      var params = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        classId: this.classId,
      };
      getArticals(params).then((res) => {
        if (res.code == 200) {
          // console.log(res);
          this.articalList = res.data;
        }
      });
    },
//.....
}

5.对tab栏内容详情的跳转和显示更多  需要在路由里面配id

   <router-link :to="'/detail/' + item.articalId">{{
              item.articalName
   }}</router-link>

加载更多

async more() {
      //加载更多
      this.currentPage++;
      var params = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        classId: this.classId,
      };
      var res = await getArticals(params);
      console.log(res);

      if (res.code == 200) {
        res.data.forEach((item, index, array) => {
          this.articalList.push(item);
        });
      } else {
        console.log("没有更多了");
      }
    },
    // more() {
    //   //用上述more方法比较好用,这个用起来还是有点问题的
    //   this.currentPage++;
    //   var aData = JSON.parse(JSON.stringify(this.articalList));
    //   var params = {
    //     currentPage: this.currentPage,
    //     pageSize: this.pageSize,
    //     classId: this.classId,
    //   };
    //   getArticals(params).then((res) => {
    //     console.log(res);
    //     res.data.forEach(function (item, index) {
    //       aData.push(item);
    //     });
    //     this.articalList = aData;
    //   });
    // },

6.详情页的跳转

7.详情页的显示

引入这两个

样式 @import + 空格+ url(CSS文件路径地址); 记得在css中引入 不然就会出问题

Vue中实现一个带有Tab切换的ECharts图表,通常需要结合Vue组件化开发的方式,以及ECharts图表库的使用。下面是一个基本的实现流程: 1. **安装ECharts**:首先需要确保你的项目中已经安装了ECharts。如果没有安装,可以通过npm或者yarn来安装。 ```bash npm install echarts --save # 或者 yarn add echarts ``` 2. **创建Vue组件**:在Vue项目中创建一个组件,例如`EChartsTab.vue`。 3. **引入ECharts**:在该组件的`<script>`部分引入ECharts。 ```javascript <script> import * as echarts from 'echarts'; export default { // 组件选项 } </script> ``` 4. **定义ECharts实例和数据**:在组件中定义ECharts实例和需要展示的数据,以及图表的配置选项。 5. **创建Tab切换功能**:通过Vue的数据和方法来创建Tab切换的逻辑。通常需要维护一个当前激活Tab的状态,并在切换时更新这个状态。 6. **渲染ECharts图表**:在Vue的`<template>`部分,使用`<div>`标签定义ECharts图表容器,并在Vue的`mounted`钩子中初始化ECharts实例,并根据当前激活的Tab来渲染相应的图表。 7. **响应Tab切换**:在`watch`或者计算属性中监听当前激活Tab的变化,并根据变化来更新ECharts实例的数据和配置,从而重新渲染图表。 8. **导出组件**:最后,导出这个Vue组件,以便在其他组件中使用。 下面是一个简化的代码示例: ```vue <template> <div> <div class="tabs"> <div v-for="tab in tabs" :key="tab.name" class="tab" :class="{ active: currentTab === tab.name }" @click="currentTab = tab.name" > {{ tab.title }} </div> </div> <div ref="echartsContainer" class="echarts-container"></div> </div> </template> <script> import * as echarts from 'echarts'; export default { data() { return { currentTab: 'first', // 当前激活的Tab tabs: [ { name: 'first', title: 'Tab 1' }, { name: 'second', title: 'Tab 2' } ], chartInstance: null }; }, mounted() { this.initChart('first'); }, methods: { initChart(tabName) { // 初始化图表或更新图表配置 if (this.chartInstance) { this.chartInstance.dispose(); // 销毁旧的实例 } const container = this.$refs.echartsContainer; this.chartInstance = echarts.init(container); const option = this.getChartOption(tabName); // 获取对应Tab的图表配置 this.chartInstance.setOption(option); }, getChartOption(tabName) { // 根据tabName返回对应的图表配置 if (tabName === 'first') { return { // 第一个Tab的ECharts配置 }; } else if (tabName === 'second') { return { // 第二个Tab的ECharts配置 }; } } } }; </script> <style> /* 你的样式 */ </style> ``` 在这个示例中,我们创建了一个带有两个Tab的简单切换组件,每个Tab对应一个ECharts图表的配置。点击不同的Tab会触发`initChart`方法,根据当前的`currentTab`来初始化或更新ECharts实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值