Vue-组件

2020-05-18  本文已影响0人  wanminglei

定义Vue组件

什么是组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用对应的组件即可; 组件化和模块化的不同:

注意: 组件中的DOM结构,有且只能有唯一的根元素(Root Element)来进行包裹!

组件中展示数据和响应事件

  1. 在组件中,data需要被定义为一个方法,例如:

    Vue.component('account', {
       template: '#tmpl',
       data() {
         return {
           msg: '大家好!'
         }
       },
       methods:{
         login(){
           alert('点击了登录按钮');
         }
       }
     });
    

【重点】为什么组件中的data属性必须定义为一个方法并返回一个对象

  1. 在子组件中,如果将模板字符串,定义到了script标签中,那么,要访问子组件身上的data属性中的值,需要使用this来访问;

使用components属性定义局部子组件

  1. 组件实例定义方式:

    <script>
     // 创建 Vue 实例,得到 ViewModel
     var vm = new Vue({
       el: '#app',
       data: {},
       methods: {},
       components: { // 定义子组件
         account: { // account 组件
           template: '<div><h1>这是Account组件{{name}}</h1><login></login></div>', // 在这里使用定义的子组件
           components: { // 定义子组件的子组件
             login: { // login 组件
               template: "<h3>这是登录组件</h3>"
             }
           }
         }
       }
     });
    </script>
    
  2. 引用组件:

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

使用flag标识符结合v-ifv-else切换组件

  1. 页面结构:

    <div id="app">
     <input type="button" value="toggle" @click="flag=!flag">
     <my-com1 v-if="flag"></my-com1>
     <my-com2 v-else="flag"></my-com2>
    </div>
    
  2. Vue实例定义:

    <script>
     Vue.component('myCom1', {
       template: '<h3>奔波霸</h3>'
     })
    
     Vue.component('myCom2', {
       template: '<h3>霸波奔</h3>'
     })
    
     // 创建 Vue 实例,得到 ViewModel
     var vm = new Vue({
       el: '#app',
       data: {
         flag: true
       },
       methods: {}
     });
    </script>
    

使用:is属性来切换不同的子组件

  1. 组件实例定义方式:

    // 登录组件
     const login = Vue.extend({
       template:
       `<div>
           <h3>登录组件</h3>
       </div>`
     });
     Vue.component('login', login);
    
     // 注册组件
     const register = Vue.extend({
       template: `<div>
         <h3>注册组件</h3>
       </div>`
     });
     Vue.component('register', register);
    
     // 创建 Vue 实例,得到 ViewModel
     var vm = new Vue({
       el: '#app',
       data: { comName: 'login' },
       methods: {}
     });
    
  2. 使用component标签,来引用组件,并通过:is属性来指定要加载的组件:

    <div id="app">
     <a href="#" @click.prevent="comName='login'">登录</a>
     <a href="#" @click.prevent="comName='register'">注册</a>
     <hr>
     <transition mode="out-in">
       <component :is="comName"></component>
     </transition>
    </div>
    
  3. 添加切换样式:

    <style>
     .v-enter,
     .v-leave-to {
       opacity: 0;
       transform: translateX(30px);
     }
    
     .v-enter-active,
     .v-leave-active {
       position: absolute;
       transition: all 0.3s ease;
     }
    
     h3{
       margin: 0;
     }
    </style>
    

父组件向子组件传值

  1. 组件实例定义方式,注意:一定要使用props属性来定义父组件传递过来的数据

    <script>
     // 创建 Vue 实例,得到 ViewModel
     var vm = new Vue({
       el: '#app',
       data: {
         msg: '这是父组件中的消息'
       },
       components: {
         son: {
           template: '<h1>这是子组件 --- {{finfo}}</h1>',
           props: ['finfo']
         }
       }
     });
    </script>
    
  2. 使用v-bind或简化指令,将数据传递到子组件中:

    <div id="app">
     <son :finfo="msg"></son>
    </div>
    

子组件向父组件传值

  1. 原理:父组件将方法的引用,传递到子组件内部,子组件在内部调用父组件传递过来的方法,同时把要发送给父组件的数据,在调用方法的时候当作参数传递进去;

  2. 父组件将方法的引用传递给子组件,其中,getMsg是父组件中methods中定义的方法名称,func是子组件调用传递过来方法时候的方法名称

    <son @func="getMsg"></son>
    
  3. 子组件内部通过this.$emit('方法名', 要传递的数据)方式,来调用父组件中的方法,同时把数据传递给父组件使用

    <div id="app">
     <!-- 引用父组件 -->
     <son @func="getMsg"></son>
    
     <!-- 组件模板定义 -->
     <script type="x-template" id="son">
       <div>
         <input type="button" value="向父组件传值" @click="sendMsg" />
       </div>
     </script>
    </div>
    
    <script>
     // 子组件的定义方式
     Vue.component('son', {
       template: '#son', // 组件模板Id
       methods: {
         sendMsg() { // 按钮的点击事件
           this.$emit('func', 'OK'); // 调用父组件传递过来的方法,同时把数据传递出去
         }
       }
     });
    
     // 创建 Vue 实例,得到 ViewModel
     var vm = new Vue({
       el: '#app',
       data: {},
       methods: {
         getMsg(val){ // 子组件中,通过 this.$emit() 实际调用的方法,在此进行定义
           alert(val);
         }
       }
     });
    </script>
    

评论列表案例

目标:主要练习父子组件之间传值

使用 this.$refs 来获取DOM元素和组件引用

  <div id="app">
    <div>
      <input type="button" value="获取元素内容" @click="getElement" />
      <!-- 使用 ref 获取元素 -->
      <h1 ref="myh1">这是一个大大的H1</h1>

      <hr>
      <!-- 使用 ref 获取子组件 -->
      <my-com ref="mycom"></my-com>
    </div>
  </div>

  <script>
    Vue.component('my-com', {
      template: '<h5>这是一个子组件</h5>',
      data() {
        return {
          name: '子组件'
        }
      }
    });

    // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
      el: '#app',
      data: {},
      methods: {
        getElement() {
          // 通过 this.$refs 来获取元素
          console.log(this.$refs.myh1.innerText);
          // 通过 this.$refs 来获取组件的数据和方法
          console.log(this.$refs.mycom.name);
        }
      }
    });
  </script>
上一篇 下一篇

猜你喜欢

热点阅读