2018-09-19

one

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Document</title>

</head>

<body>

  <div id='app'>

      <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:` 

                  <ul>

                        <li>

                            <a href="">首页</a>

                        </li>

                        <li>

                            <a href="">详情页页</a>

                        </li>

                  </ul>

            `

        })

      new Vue({

          el:'#app'

          data:{},

          methods:{},

          filters:{},

          computed:{},

          components:{

              'my-component':{

                  template:``

              }

          }   

      })

    </script>

</body>

</html>

know

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Document</title>

</head>

<body>

  <div id='app'>

      <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:` 

                  <ul>

                        <li>

                            <a href="">首页</a>

                        </li>

                        <li>

                            <a href="">详情页页</a>

                        </li>

                  </ul>

            `

        })

      new Vue({

          el:'#app'

          data:{},

          methods:{},

          filters:{},

          computed:{},

          components:{

              'my-component':{

                  template:``

              }

          }   

      })

    </script>

</body>

</html>



组件2

<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>

                  <p>{{mess}}</p>

                  <button @click='alt'>按钮</button>

              </div>

        `,

        data:function(){

            return{

                mess:'我是组件中的值'

            }

        },

        methods:{

            alt:function(){

                alert('bdsjjf')

            }

        }

    }) 

    new Vue({

        el:"#app",

        data:{

            msg:'jsdkvg'

        },

        methods:{

        }

    })

    </script>

</body>

</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>

                  <p>{{mess}}</p>

                  <button @click='alt'>按钮</button>

              </div>

        `,

        data:function(){

            return{

                mess:'我是组件中的值'

            }

        },

        methods:{

            alt:function(){

                alert('bdsjjf')

            }

        }

    }) 

    new Vue({

        el:"#app",

        data:{

            msg:'jsdkvg'

        },

        methods:{

        }

    })

    </script>

</body>

</html>



组件之间的传值~父传子

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Document</title>

</head>

<body>

  <div id='app'>

      <my-father></my-father>

  </div>

  <script src='js/vue.js'></script>

  <script>

      Vue.component('my-father',{

          template:`

                <div>

                    <my-tit v-bind:tit='title'></my-tit>

                    <my-fruit v-bind:fruList='list'></my-fruit>

                </div>

          `,

          data:function(){

              return{

                  list:['apple','pear','banana'],

                  title:'水果列表'

              }

          }

      })


      Vue.component('my-tit',{

          props:['tit'],

          template:`

                <h2>{{tit}}</h2>

              `

      })



      Vue.component('my-fruit',{

          props:['fruList'],

          template:`

                <ul>

                    <li v-for="value in fruList">{{value}}</li>

                </ul>

            `

      })



      new Vue({

          el:'#app'

      })


    </script>

</body>

</html>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 组件(Component)是Vue.js最核心的功能,也是整个架构设计最精彩的地方,当然也是最难掌握的。...
    六个周阅读 5,657评论 0 32
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,086评论 0 29
  • 夏天到了,皮肤容易长粉刺。粉刺分为白头粉刺和黑头粉刺,有的是“开放式”的,有的是“闭合式”的。粉刺通常分布在额头、...
    teloumo阅读 529评论 1 0
  • 周末好不容易有空,去超市采购,路上必经的是公园,总是会去绕一圈才去买买买。抬头偶然发现有了些许黄叶很是开心。身在云...
    阿瑟流士阅读 193评论 0 0
  • 今早在校值班,接待了H母子。母子俩赴美一年,今天回到母校,跟我交谈甚欢,直到正午他们才在孔子塑像和校门口...
    浅浅风阅读 216评论 1 4