美文网首页
21vue- 自定义标签

21vue- 自定义标签

作者: 滔滔逐浪 | 来源:发表于2021-08-09 22:54 被阅读0次
    
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <script src="../../lib/vue.js"></script>
            <script src="../../lib/axios.js"></script>
        </head>
        <body>
            <div id="app">
                 <div v-hello=" 'red' ">454445</div>
                 <div  v-hello=" 'blue' ">1111111111</div>
                 <div  v-hello=" 'yellow' ">222222222222222</div>
                  <div  v-hello="mycolor">3333333333333</div>
            </div>
    
    <script>
        
        Vue.directive("hello",function(el,binding){
            el.style.background=binding.value
        })
        /* Vue.directive("hello",{
            //指令的生命周期
            //创建
            inserted(el,binding){
            //  console.log("绑定当前指令的标签插入父节点就会执行",binding)
                el.style.background=binding.value
            },
            //更新
            update(el,binding){
                console.log("绑定当前指令的标签插入父节点就会执行1",binding)
                el.style.background=binding.value
            },
        }) */
        new Vue({
            el:"#app",
            data:{
                mycolor: "red"
                
            }
            
        });
    </script>
        </body>
    </html>
    
    

    指令轮播

    
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
      <link rel="stylesheet" href="../05%20swiper/swiper/css/swiper.css" />
              <script src="../05%20swiper/swiper/js/swiper.js"></script>
            <script src="../../lib/vue.js"></script>
            <script src="../../lib/axios.js"></script>
        </head>
        <style>
            .swiper-container{
                width:600px;
                height: 300px;
            }
        </style>
        <body>
            <div id="app"> 
            
            <div class="swiper-container kerwin">
              <div class="swiper-wrapper">
              <div class="swiper-slide" v-for="(data, index) in datalist" v-swiper="{current:index, length: datalist.length}">        
                  {{data}}
              </div>
              <div name="swiper-pagination"></div>
              </div>
            </div>
         
            </div>
    
    <script>
        //vnode 虚拟节点
      Vue.directive("swiper",{
          //vnode 指令节点
          inserted(el,binding,vnode){
                console.log(vnode.context.datalist.length);
                if(binding.value.current===binding.value.length-1){
                    new Swiper(".kerwin",{
                                  loop:true,
                                  pagination:{
                                      el:".swiper-pagination"
                                  }
                    })    
                }
                
              
            
          }
          
          
      })
      
        new Vue({
            el:"#app",
            data:{
                datalist:[],
            },
            mounted(){
                setTimeout(()=>{
                    this.datalist=["111111","22222","3333333"];
                //状态同步更新
                },2000)
            }
            
        });
    </script>
        </body>
    </html>
    
    
    

    $nextTick

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>轮播nextTick</title>
      <link rel="stylesheet" href="../05%20swiper/swiper/css/swiper.css" />
              <script src="../05%20swiper/swiper/js/swiper.js"></script>
            <script src="../../lib/vue.js"></script>
            <script src="../../lib/axios.js"></script>
        </head>
        <style>
            .swiper-container{
                width:600px;
                height: 300px;
            }
        </style>
        <body>
            <div id="app"> 
            
            <div class="swiper-container kerwin">
              <div class="swiper-wrapper">
              <div class="swiper-slide" v-for="(data, index) in datalist">        
                  {{data}}
              </div>
              <div name="swiper-pagination"></div>
              </div>
            </div>
         
            </div>
    
    <script>
        
      
        new Vue({
            el:"#app",
            data:{
                datalist:[],
            },
            updated(){
                console.log("updated")
            },
            mounted(){
                setTimeout(()=>{
                    
                        this.datalist=["111111","22222","3333333"];
                        this.$nextTick(()=>{
                            console.log("我执行的比updated都晚只会更新一次")
                            new Swiper(".kerwin",{
                                        loop:true,
                                        pagination:{
                                                                          el:".swiper-pagination"
                                        }           
                                                })   
                            
                        })
                    
                },2000)
            },
            
        })
    </script>
        </body>
    </html>
    
    
    
    

    相关文章

      网友评论

          本文标题:21vue- 自定义标签

          本文链接:https://www.haomeiwen.com/subject/mebtbltx.html