美食杰实现菜谱大全功能

1.效果展示

在这里插入图片描述

1.1需要实现的效果

1.先进行数据渲染
2.点击家常菜谱跳转对应的路由
3.点击当前添加对应的背景颜色

2.在recipe.vue页中写

    <div class="recipe">
        <!-- v-model="activeName" -->
        <!-- 菜谱分类 start -->
        <el-tabs  type="border-card" v-model="classifyName">
            <el-tab-pane 
                v-for="item in classify" 
                :key="item.parent_type"  
                :label="item.parent_name" 
                :name="item.parent_type"
                >
                <div class="recipe-link">
                      <router-link v-for="option of item.list"
                            :to='{query:{...$router.query,classify:option.type}}'
                            :key="option.type"
                            :class="{active:classifyType == option.type}"
                        >
                       {{option.name}}
                    </router-link>
                </div>
            </el-tab-pane>
        </el-tabs>
        <!-- 菜谱分类 end -->
        <h2>家常好味道,给你家一般的温暖</h2>
<script>
    import MenuCard from '@/components/menu-card.vue'
    import {getClassify, getProperty, getMenus} from '@/service/api';

    export default {
        components: {MenuCard},
        data() {
            return {
                classify:[],//存储tab切换的所有数据
                classifyType:'1-1',//tab切换选中项(二级路由)
                classifyName:'1',//定义刷新tab得值(一级路由)
                //属性
                properties:[], //存储属性中的所有数据
                propertyType:{},//存储属性的分类,例如:{cr:1-4,fc=2-1}
                propertyActiveName:[]
            }
        },
        watch: {
            $route: {
                handler(){
                    const {classify}=this.$route.query;
                    if(classify){
                       this.classifyType=classify; //1-1
                       this.classifyName=classify[0];  //1
                    }
                    this.ThisgetMenus
                },
                immediate:true
            }
        },
        mounted() {
            getClassify().then(({data})=>{
                console.log(data);
                this.classify=data;
                if(!this.$route.query.classify){
                   this.classifyType=this.classify[0].list[0].type;
                   this.classifyName=this.classify[0].parent_type;
                   this.$router.push({
                       query:{
                         classify:this.classifyType,
                         page:1
                       }
                   })
                }
            });
            getProperty().then(({data})=>{
            //    console.log(data);
               this.properties=data;
               const {query} = this.$route;
                this.propertyType = this.properties.reduce((o,item)=>{
                    //  item.title :  工艺,难度,口味,人数
                    o[item.title] = query[item.title] ? query[item.title]: "";
                    if(o[item.title]){
                        this.propertyActiveName.push(o[item.title][0])
                    }
                    return o;
                },{});
            })
        },
        methods: {
           selectedTag(option){
              let query={...this.$route.query}
              //判断是否点击,如果点击过:取消,否则选中
              if(this.propertyType[option.title]==option.type){
                  this.propertyType[option.title]="";
                  delete query[option.title];
              }else{
                  this.propertyType[option.title]=option.type;
                  query[option.title]=option.type;
              }
              this.$router.push({
                  query

              })
           },                  

           ThisgetMenus(){
               const query ={...this.$route.query}
               const params={
                   page:query.page||'1',
                   classify:query.classif
               }
               getMenus(params).then(({data})=>{
                  console.log(data)
               })
                 
               
           }

        }
    }
</script>

3.效果展示

在这里插入图片描述
3.1先进行数据渲染
绑定点击事件,判断是否点击,如果点击过:取消,否则选中添加背景颜色

<el-container>
            <el-aside width="220px" class="recipe-aside">
                <div class="filter-box">
                    <h4>筛选</h4>
                    <!-- v-model="activeName" -->
                    <!-- 筛选 start -->
                    <el-collapse v-model="propertyActiveName">
                        <el-collapse-item
                            v-for="item of properties"
                            :key="item.parent_type"
                            :title="item.parent_name"
                            :name="item.parent_type"
                        >
                            <div class="filter-tags">
                                <el-tag type="info"
                                    v-for="option in item.list"
                                    :key="option.type"
                                    @click="selectedTag(option)"
                                    :class="{'tag-selected':propertyType[option.title]===option.type}"
                                >
                                {{option.name}}
                                </el-tag>
                            </div>
                        </el-collapse-item>
                    </el-collapse>
                    <!-- 筛选 end -->
                </div>
            </el-aside>
            <el-main class="filter-menus-box">
<!--                <div class="menu-empty">暂时没有过滤出菜谱信息,请选择其他的筛选条件</div>-->
                <menu-card style="min-height: 75%;" :info="list"></menu-card>
                <div style="text-align: right;">
                    <el-pagination
                            style="display: inline-block;"
                            page-size="10"
                            layout="total, prev, pager, next"
                            :total="50">
                    </el-pagination>
                </div>
            </el-main>
        </el-container>
    </div>
</template>
<script>
    import MenuCard from '@/components/menu-card.vue'
    import {getClassify, getProperty, getMenus} from '@/service/api';

    export default {
        components: {MenuCard},
        data() {
            return {
                classify:[],//存储tab切换的所有数据
                classifyType:'1-1',//tab切换选中项(二级路由)
                classifyName:'1',//定义刷新tab得值(一级路由)
                //属性
                properties:[], //存储属性中的所有数据
                propertyType:{},//存储属性的分类,例如:{cr:1-4,fc=2-1}
                propertyActiveName:[]
            }
        },
        watch: {
            $route: {
                handler(){
                    const {classify}=this.$route.query;
                    if(classify){
                       this.classifyType=classify; //1-1
                       this.classifyName=classify[0];  //1
                    }
                    this.ThisgetMenus
                },
                immediate:true
            }
        },
        mounted() {
            getClassify().then(({data})=>{
                console.log(data);
                this.classify=data;
                if(!this.$route.query.classify){
                   this.classifyType=this.classify[0].list[0].type;
                   this.classifyName=this.classify[0].parent_type;
                   this.$router.push({
                       query:{
                         classify:this.classifyType,
                         page:1
                       }
                   })
                }
            });
            getProperty().then(({data})=>{
            //    console.log(data);
               this.properties=data;
               const {query} = this.$route;
                this.propertyType = this.properties.reduce((o,item)=>{
                    //  item.title :  工艺,难度,口味,人数
                    o[item.title] = query[item.title] ? query[item.title]: "";
                    if(o[item.title]){
                        this.propertyActiveName.push(o[item.title][0])
                    }
                    return o;
                },{});
            })
        },
        methods: {
           selectedTag(option){
              let query={...this.$route.query}
              //判断是否点击,如果点击过:取消,否则选中
              if(this.propertyType[option.title]==option.type){
                  this.propertyType[option.title]="";
                  delete query[option.title];
              }else{
                  this.propertyType[option.title]=option.type;
                  query[option.title]=option.type;
              }
              this.$router.push({
                  query

              })
           },                  

           ThisgetMenus(){
               const query ={...this.$route.query}
               const params={
                   page:query.page||'1',
                   classify:query.classif
               }
               getMenus(params).then(({data})=>{
                  console.log(data)
               })
                 
               
           }

        }
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值