2018-09-18vue父?jìng)髯咏M件的幾個(gè)案例

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
       <title>Document</title>
    </head>
    <body>
    <!--    
    *****組件:組件可以擴(kuò)展 HTML 元素,封裝可重用的代碼。 
       ***組件之間的傳值


    -->
    <script>

      var uname='jack';
      var age=18;
      console.log('我的名字叫'+uname+','+'我的年齡是'+age+'歲');
      console.log(`我的名字叫${uname},我的年齡是${age}歲了`);
    </script>
    </body>
    </html>




     <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
       <div id='itany'>
           <father></father>
       </div>
        <script src='js/vue.js'></script>
        <script>
            Vue.component('father',{
                template:`
                    <div>
                        <h1>這是父元素</h1>
                        <child v-bind:number='num'></child>
                        <button @click='add'>點(diǎn)擊加1</button>
                    </div>
                `,
                data:function(){
                    return{
                        num:1
                    }
                },
                methods:{
                    add:function(){
                        this.num++;
                    }
                }
            })
    
            Vue.component('child',{
                props:['number'],
                template:`
                     <div>
                          <h3>這是子元素</h3>
                          <a href='#'>{{number}}</a>
                     </div>

                  `
            })
           new Vue({
               el:'#itany'
           })
        </script>
    </body>
    </html>




    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
     <div id='app'>
         <cont></cont>
     </div>
      <script src='js/vue.js'></script> 
      <script>
          Vue.component('cont',{
              template:`
                  <div>
                     <h1>這是父元素</h1>
                     <top-title v-bind:fruTit='tit'></top-title>
                     <list v-bind:fruList='fruit'></list>
                  </div>
               `,
              data:function(){
                  return{
                      fruit:['apple','pear','banana'],
                      tit:'水果列表'
                  }
              }
          })
          //子組件1
          Vue.component('top-title',{
              props:['fruTit'],
              template:`
                     <h3>{{fruTit}}</h3>    
                 `
          })
  
          //子組件
          Vue.component('list',{
              props:['fruList'],
             template:`
                <ul>
                   <li v-for="value in fruList">{{value}}</li>
                </ul>
              `            
          })
         new Vue({
             el:'#app'
         })  
      </script> 
    </body>
    </html>

水果列表

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
       <div id='app'>
           <my-component></my-component>
       </div>
        <script src='js/vue.js'></script>
        <script>
          Vue.component("my-component",{
              template:`
                   <div>
                       <input type='text' v-model='list'>  
                       <button @click='add'>添加</button>
                       <my-child v-bind:fruit='fruList'></my-child>
                   </div>

             `,
              methods:{
                  add:function(){
                     this.fruList.push(this.list);
                      this.list=''
                  }
              },
              data:function(){
                  return{
                      fruList:['apple','pear','banana'],
                      list:''
                  } 
              }
          })  
    
  
          Vue.component('my-child',{
              props:['fruit'],
               template:`
                   <ul>
                      <li v-for="(value,index) in fruit">
                        {{value}}
                        <button @click='delt(index)'>刪除</button>
                    </li>
                 </ul>
               `,
              methods:{
                  delt:function(ind){
                      this.fruit.splice(ind,1)  
                  }
              }
         })
    
           new Vue({
               el:'#app'
           })
        </script>
    </body>
    </html>






    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
       <div id='app'>
    <!--       <p>{{msg}}</p>-->
           <my-component></my-component>
           <my-component></my-component>
           <my-component></my-component>
           <my-component></my-component>
           <my-component></my-component>
           <my-component></my-component>
           <my-component></my-component>
       </div>
        <script src='js/vue.js'></script>
        <script>
            //全局組件
    //      Vue.component('my-component',{
    //          template:`
    //               <div>
    //                 
    //               </div>
    //           `
    //          
    //      })  
           new Vue({
               el:'#app',
               data:{
                   msg:'jdkfg'
               },
               methods:{},
               computed:{},
               filters:{},
               components:{
                   'my-component':{
                       template:`
                            <div>
                               <p>
                                  <a href='#'> 去百度</a>
                               </p>
                            </div>
                       `
                   }
               }
           })

        </script>
    </body>
    </html>






    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
      <div id='app'>
           <my-component></my-component>
  
       </div>
        <script src='js/vue.js'></script>
        <script>
            Vue.component('my-component',{
                template:`
                    <div>
                        <h1>{{msg}}</h1>
                        <button @click='alt'>點(diǎn)擊按鈕</button>
                         <my-child></my-child>

     
                    </div>
          `,
                data:function(){
                    return{
                        msg:'我是組件中的內(nèi)容'
                    }
                },
                methods:{
                    alt:function(){
                        alert(11111)
                    }
                }

           })
    
            Vue.component('my-child',{
                template:`
                      <div>
                          <h3>我是第二個(gè)組件</h3> 
                          <p>hbjhjhjbbhjjhbjhbjhbhbjhbjjhb</p>
                      </div>
                  `
            })
    
            new Vue({
                el:"#app",
                data:{},
                methods:{}
            })
        </script>
    </body>
    </html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容