Simple switching tab vue

When using the vue often used tab switching, demo below

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<style>
    .tab_box{
        display: flex;
        margin: 0 auto;
        width: 500px;
    }
    .tab{
        width: 100px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        color: #ffffff;
        background: #000000;
    }
    .tab_content{
        margin: 0 auto;
        width: 500px;
        height: 300px;
        text-align: center;
        line-height: 300px;
        background: yellow;
        color: #000000;
        overflow: hidden;
    }
    .tab_content_item{
        height: 100%;
        width: 100%;
    }
    .active{
        background: red;
        color: #000000;
    }
</style>
<body>
    <div id="app">
        <div class="tab_box">
            <! - loop tab header (item, index) item for the variable name index for the index @ click = "tabClick (index)" setting Click event passed in the index because the only index that is controlled only: class = "? num == index 'active': ''" triplet of expressions to satisfy the conditions to add the class name does not satisfy not add ->
            <div v-for ="(item,index) in list" class="tab" @click="tabClick(index)" :class = "num==index? 'active':''">
                {{item.name}}
            </div>
        </div>
        <div class="tab_content">
                <-! V-if = "num == index" which is initialized to 0 num tabs body definition is to make the first display additional deletion here of course also be used v-show effect is the same, but need to be addressed css because v-show he is accounted location ->
            <div class="tab_content_item" v-for ="(item,index) in list" v-if = "num==index">
                I am interested in {{item.age}} is {{item.hobby}}
            </div>
        </div>
    </div>
</body>
    <script>
        new view ({  
            the '#app'  
            data: {
                // write data list only one, according to the requirements tab tab head and body may be two objects, the same principle
                list : [
                    {Name: 'Bob', age: 23, hobby: 'fight'},
                    {Name: 'small strong', age: 25, hobby: 'basketball'},
                    {Name: 'Xiaopeng', age: 23, hobby: 'comic'},
                    {Name: 'Li', age: 23, hobby: 'sleep'},
                    {Name: 'Betty', age: 23, hobby: 'smug'}
                ],
                // define the initial lower standard style that is initialized to show the page
                a: 0
            },
            methods:{
                // Set the click event is received index
                tabClick:function(index){
                    console.log(index)
                    // m-per-click when reassigned num 
                    this.num = index
                }
            }  
        })
    </script>
</html>
Results are as follows
If you really do not understand you can copy the code to run it, hands is the last word

 

Guess you like

Origin www.cnblogs.com/qq976864507/p/11803755.html