vue-选项卡案例->动态获取数据

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <link rel="stylesheet" href="css/reset.css"> -->
    <title>vue-选项卡案例</title>
    <style>
        .tabBox{
            box-sizing: border-box;
            width: 600px;
            margin: 20px auto;
        }

        .tabBox .tab{
            display: flex;
            position: relative;
            top: 1px;
            margin: 0;
            padding: 0;
        }

        .tabBox .tab li {
            list-style: none;
            padding: 0;
            margin: 0;
            padding: 0 20px;
            line-height: 35px;
            margin-right: 10px;
            border: 1px solid #aaa;
            background-color: #eee;
            cursor: pointer;
        }

        .tabBox .tab li.active{
            background-color: #fff;
            border-bottom-color: #fff ;
        }

        .tabBox .content{ 
            box-sizing: border-box;
            padding: 10px;
            height: 300px;
            border: 1px solid #aaa;
        }
  
    </style>
</head>
<body>
    <div id="app">
        
         <div class="tabBox"> 
             <ul class="tab">
                <li   @click="handle($event,index,item.id)" :class="{active:curIndex === index}"
                 v-for="(item,index) in TAB_DATA"
                 v-text="item.name"   ></li>   
             </ul> 
             <div class="content" v-html="content"></div> 
         </div>

    </div>
 
    <script src="../../node_modules/vue/dist/vue.js"></script>  
    <script> 

        let data_content = [
                {
                    "id":1,
                    "content":"音乐的内容"
                },
                {
                    "id":2,
                    "content":"影视的内容"
                },
                {
                    "id":3,
                    "content":"动漫的内容"
                },
                {
                    "id":4,
                    "content":"纪录片的内容"
                }
            ];
  
        let TAB_DATA = [
          {
            id:1,
            name:'音乐' 
          },
          {
            id:2,
            name:'影视' 
          },
          {
            id:3,
            name:'动漫' 
          },
          {
            id:4,
            name:'纪录片' 
          }
        ];

        let vm = new Vue({
            el: "#app",
            data: {
                // 动态绑定的数据 
                TAB_DATA,
                // 展示选项卡的索引
                curIndex:0,
                // 内容区域的数据
                content:''
            }, 
            created(){
                this.queryData(this.TAB_DATA[this.curIndex]['id'])
            },
            methods:{ 
               queryData(curId){
                   let itemData = data_content.find(item=>parseInt(item.id) === parseInt(curId)); 
                   if(itemData){
                     this.content = itemData.content
                     return ;
                   } else{
                    this.content = '查无此信息'
                   }
               },
               handle(ev,index,id){   
                if(this.curIndex == index) return;
                this.curIndex = index;
                this.queryData(id);
               }
            } 
        }); 

    </script>
 
</body>
</html>
posted @ 2021-03-01 11:37  13522679763-任国强  阅读(392)  评论(0)    收藏  举报