美文网首页
与Vue.js的第六天

与Vue.js的第六天

作者: 李泽裕 | 来源:发表于2018-09-20 15:18 被阅读0次

今天学习了组件

组件

组件(ccomponent):是vue最强的的功能之一。
组件可以扩展 HTML 元素,封装可重用的代码
组件分为全局组件局部组件

全剧组件写法

<body>
   <div id='app'>
       <my-component></my-component>
       <ul>
           <li></li>
       </ul>
   </div>
    <script src='js/vue.js'></script>
    <script>
        Vue.component('my-component',{
            template:`
                <div>
                    <h1>这是一个组件</h1>
                        <ul>
                            <li>1111</li>
                            <li>1111</li>
                            <li>1111</li>
                            <li>1111</li>
                        </ul>
                 </div>     
            `
        }) 
        new Vue({
            el:'#app',
        })
    </script>
</body>

局部组件写法

<body>
   <div id='app'>
       <my-component></my-component>  
       <ul>
           <li></li>
       </ul>
      
   </div>
    <script src='js/vue.js'></script>
    <script>
        new Vue({
            el:'#app',
            components:{
                'my-component':{
                    template:`
                        <h1>这是一个组件</h1>
                    `
                }
            }
        })
    
    </script>
</body>

实例

<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'>按钮</button>
                </div>
              `,
           data:function(){
               return{
                   msg:'dcgf'
               }
           },
           methods:{
               alt:function(){
                   alert(11111) 
               }
           }
       })
       new Vue({
           el:'#app',
           
       })
    </script>
</body>

组件嵌套

父传子

实例

<body>
   <div id='app'>
       <my-content></my-content>
       
   </div>
    <script src='js/vue.js'></script>
    <script>
       Vue.component("my-content",{
           
           template:`
              <div>
                 <h2>我是my-content组件的标题</h2>
                 <my-child v-bind:message='msg'></my-child>

             </div>
            `,
            data:function(){
                return{
                    msg:'dgddbghfghfnh'
                }
            }
       }) 
       
       Vue.component("my-child",{
           props:['message'],
           template:`
              <div>
               <h3>我是my-child组件中的标题</h3>
               <p>{{message}}</p>
              </div>
            `
       })
       new Vue({
          el:'#app'
       })
    
    </script>
</body>

水果列表

<body>
   <div id='app'>
       <my-father></my-father>
   </div>
    <script src='js/vue.js'></script>
    <script>
        Vue.component("my-father",{
            template:`
               <div>
                 <my-son v-bind:tit='title'></my-son>
                 <my-list v-bind:fruit='arr'></my-list>
              </div>
            `,
            data:function(){
                return{
                    arr:['apple','pear','orange'],
                    title:'水果列表'
                }
            }
        })
        //title
        Vue.component('my-son',{
            props:['tit'],
            template:`
                    <h2>{{tit}}</h2>
               `
        })
        //arr
        Vue.component('my-list',{
            props:['fruit'],
            template:`
                  <ul>
                      <li v-for="value in fruit">{{value}}</li>
                 </ul>
            `
        })
        
       new Vue({
           el:'#app'
       })
    </script>
</body>

相关文章

网友评论

      本文标题:与Vue.js的第六天

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