封装一个不同小标题对应不同的文字颜色

 我们在做列表渲染的时候 经常会根据额后端给的数据字典来判断属于什么  如果说文字都是一个颜色 我们可以不用判断,但是想么麻烦的 不能在页面每个都要判断 这样就太麻烦了 ,所以要封装一个类型 

 封装一个 style.js 文件

export function getStyle(text) {
  let style = {};
  switch (text) {
    case '公园游乐园':
      style = {
        height: '28rpx',
        background: '#4ccfff',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '106rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
    case '名胜古迹':
      style = {
        height: '28rpx',
        background: '#FFAB19',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
    case '度假观光':
      style = {
        height: '28rpx',
        background: '#299CED',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
    case '宗教圣地':
      style = {
        height: '28rpx',
        background: '#FF5400',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
    case '红色经典':
      style = {
        height: '28rpx',
        background: '#FF1919',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
    case '自然风光':
      style = {
        height: '28rpx',
        background: '#15AF00',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
    case '名胜推荐':
      style = {
        height: '28rpx',
        background: '#AE00FF',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };

      break;
    case '动植物园':
      style = {
        height: '28rpx',
        background: '#00AF88',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };

      break;
    case '影视基地':
      style = {
        height: '28rpx',
        background: '#8A6959',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };

      break;
    case '特色街区':
      style = {
        height: '28rpx',
        background: '#B63C00',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };

      break;
    case '博物馆':
      style = {
        height: '28rpx',
        background: '#1d71fc',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
    case '故居纪念馆':
      style = {
        height: '28rpx',
        background: '#741E1E',
        borderRadius: '6rpx',
        fontSize: '20rpx',
        color: '#fff',
        width: '98rpx',
        lineHeight: '28rpx',
        textAlign: 'center'
      };
      break;
  }
  return style;
}

 

<template>
  <view>
    <view class="Attractions_box" v-for="(item,index) in HotList" :key="index" @click="onMapClick(item.id)">
      <view class="Attractions_hot">
        <image src="../../../static/homePage/hot.png" mode="" class="hot_icon" v-if="item.isHot"></image>
      </view>
      <view class="Attractions_box_left">
        <image :src="item.coverImg" mode="" class="Attractions_icon"></image>
      </view>
      <view class="Attractions_box_right">
        <view class="Content_details">
          <view class="Content_details_up">
            <text class="details_name">{{item.name}}</text>
            <view class="distance" v-if="item.distance">
              <image src="../../../static/homePage/navigation.png" mode="" class="navigation_iocn"></image>
              <text class="km_distance">{{ getIntegerDistance(item.distance) }}</text>
            </view>
          </view>
          <view class="particulars">{{item.description}}</view>
          <view class="distance distance_tag">
            <!-- 样式修改 如果没有几级景区 ,单独写一个样式 -->
            <view class="label_box_level" v-if="item.level">{{item.level}}A</view>
            <!-- #ifdef APP -->
            <view :style="getDynamicStyle(item.tag)">{{item.tag}}</view>
            <!-- #endif -->
          </view>
          <view class="distance">
            <view class="explain_box">
              <image src="../../../static/homePage/listine.png" mode="" class="listine_icon"></image>
              <text class="listine_text"><text class="demo1">{{item.markerNum}}</text>处讲解</text>
              <image src="../../../static/homePage/line.png" mode="" class="line_cion"></image>
              <text class="go_explain"><text class="demo1">{{item.viewNum?item.viewNum:'10'}}</text>w人去过</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    getStyle
  } from '../../../utils/style.js'
  export default {
    props: {
      ScenicListArry: {
        type: Array
      }
    },
    data() {
      return {
        HotList: [],
        expireDate: '',
        style: {}
      }
    },
    watch: {
      ScenicListArry: {
        deep: true,
        handler(nVal, oVal) {
          if (nVal) {
            this.HotList = nVal

          }
        },
      },
    },
    computed: {
      getDynamicStyle() {
        return function(tag) {
          return getStyle(tag);
        };
      }
    },
    methods: {
      getIntegerDistance(distance) {
        // 将距离从米转换为千米并保留一位小数
        const kmDistance = (distance / 1000).toFixed(1); // 将距离除以1000并保留一位小数
        return kmDistance + 'km';
      }


    }
  }
</script>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值