2018-09-20

1,VUE组件

简介:组件(Component)是 Vue.js 最强大的功能之一。
作用:组件可以扩展 HTML 元素,封装可重用的代码。
2,VUE写法
组件也分为两种:
1,全局组件:
举例:

    <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>  
   <ul>
       <li></li>
   </ul>
  
       </div>
        <script src='js/vue.js'></script>
        <script>
          //全局:
        Vue.component('my-component',{
            template:`
              <ul>
               <li></li>
              </ul>

            `
        })
    
        new Vue({
            el:'#app',
                }
            }
        })

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

2,局部组件:
实例:

  <div id="app">
    <runoob></runoob>
    </div>

  <script>
  var Child = {
    template: '<h1>自定义组件!</h1>'
  }

    // 创建根实例
    new Vue({
    el: '#app',
    components: {
      // <runoob> 将只在父模板可用
      'runoob': Child
    }
  })
  </script>

2,组件的简单运用·

例如:使用vue组件来做一个点击事件,点击时弹出 hello world

   <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'>按钮</button>
                  </div>
                  `,
             data:function(){
               return{
                   msg:'dcgf'
                   }
             },
             methods:{
                 alt:function(){
                     alert(‘hello,world’) 
                 }
           }
         })
             new Vue({
             el:'#app',
               
         })
      </script>
  </body>
  </html>     
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 1,190评论 0 1
  • 1.组件:是vue最强大的功能之一,组件化开发,可扩展HTML元素,封装可重用的代码。 2.组件之间的传值:父传子...
    Alive_92dc阅读 147评论 0 0
  • 组件(component):组件化开发 组件可以扩展 HTML 元素,封装可重用的代码。全局组件 局部组件注: 组...
    其实_dnhl阅读 193评论 0 0
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,088评论 0 29
  • awesome-github-vue 是由OpenDigg[https://blog.csdn.net/opend...
    我是七月阅读 2,438评论 0 20