Vue组件

本文详细介绍了Vue.js中的组件创建与使用,包括全局组件和局部组件的注册,单文件组件的结构,以及父子组件间的数据传递方法,如props、自定义事件和ref属性的运用。通过实例展示了如何在Vue应用中实现组件的复用和通信。
摘要由CSDN通过智能技术生成

VUE组件

全局组件

注意事项

  • 组件中的data必须是一个函数,这样就能让各个组件的数据独立。
  • template必须有个明确的根元素,简单说就是最外层必须是一个标签,还可以使用ES6模板语法
  • 可以是用短横线的方式命名,也可以使用驼峰的方式(驼峰的方式,在组件的模板中可以直接使用,但是在vue的html代码中必须使用横线的方式使用)
// 注册组件
// Vue.component('BtnCounter',function(){
Vue.component('btn-counter',function(){
    // 组件中的data必须是一个函数
    data: function(){
        retrun {
            count: 0
        }
    },
    // template必须有个明确的根元素,简单说就是最外层必须是一个标签
    // template: '<button @click="count++">点击了{{count}}次</button>',
    template: '<button @click="handle">点击了{{count}}次</button>',
    methods: {
        handle: function(){
            this.count++;
        }
    }
});
    
// 组件使用
<div id="app">
    <button-counter></button-counter>
</div>

局部组件

局部组件只能在注册它的父组件中使用

  • 方式1
const HelloWorld = Vue.extend({
  template: '<div>{{ msg }}</div>',
  data: function () {
    return {
        msg: 'helloworld'
    }
  }
})

var vm = new Vue({
    el:'#app',
    data: {
        
    },
    components: {
        'hello-world': HelloWorld
    }
});
    
<div id="app">
    <hello-world></hello-world>
</div>
  • 方式2(这是方式1的简写版本,看似没有调用extend,但是其实vue在底层帮你调用了)
var HelloWorld = {
    data: function(){
        retrun {
            msg: 'helloworld'
        }
    }
    template:'<div>{{msg}}</div>'
}
    
var vm = new Vue({
    el:'#app',
    data: {
        
    },
    components: {
        'hello-world': HelloWorld
    }
});
    
<div id="app">
    <hello-world></hello-world>
</div>

单文件组件

// 子组件
<template>
    <div>
        <h1 class="hi">{{ msg }}</h1>
    </div>
</template>
<script>
export default {
    name: 'children',
    data() {
        return {
            msg: "欢迎"
        }
    }
}
</script>
<style>
.hi {
    background-color: black;
}
</style>

// 父组件
<template>
    <div id="app">
        <children></children>
    </div>
</template>

<script>
import children from './children'
new Vue({
    el:'#app',
    data: {
        
    },
    components: {
        children
    }
});
</script>

父子组件传值

父传子

使用props,详见props介绍

子传父

1.函数形式

// 子组件
<template>
    <div>
      <h1>{{ msg }}</h1>
      <div>姓名:{{name}}</div>
      <div>地址:{{address}}</div>
      <button @click="sendInfo">把子组件数据给父组件</button>
    </div>
</template>
<script>

import mixin from './mixin/mixin'

export default {
  name: 'Student',
  data () {
    return {
      msg: 'Welcome',
      name: '仙剑',
      address: '余杭镇'
    }
  },
  props: ['getChildInfo'],
  methods: {
      sendInfo() {
          this.getChildInfo(this.name)
      }
  }
}
</script>

// 父组件
<template>
  <div>
    <School :getChildInfo="getChildInfo"></School>
  </div>
</template>

<script>
import School from './School.vue'

export default {
  name: 'HelloWorld',
  data () {
    return {
    }
  },
  components: {
    School
  },
  methods: {
    getChildInfo(val) {
      console.log("这是子组件给我的数据", val)
    }
  }
}
</script>
  1. 自定义事件形式
// 子组件
<template>
    <div>
      <h1>{{ msg }}</h1>
      <div @click="handleClick">姓名:{{name}}</div>
      <div>姓名:{{childName}}</div>
      <div>年龄:{{age}}</div>
      <button @click="sendInfo">把子组件数据给父组件</button>
    </div>
</template>
<script>

import mixin from './mixin/mixin'

export default {
  name: 'Student',
  data () {
    return {
      msg: 'Welcome',
      childName: "随便de"
    }
  },
  mixins: [mixin],
  props: {
    name: {
      type: String,
      requried: true
    },
    age: {
      type: Number,
      default: 18
    }
  },
  methods: {
    sendInfo() {
      this.$emit("getchildrenInfo",this.childName)
    }
  }
}
</script>


// 父组件
<template>
  <div>
    <Student name="景天" :age="20" @getchildrenInfo="getchildrenInfo"></Student>
  </div>
</template>

<script>
import Student from './Student.vue'

export default {
  name: 'HelloWorld',
  data () {
    return {
    }
  },
  components: {
    Student
  },
  methods: {
    getchildrenInfo(val) {
      console.log("这是子组件给我的数据", val)
    }
  }
}
</script>
  1. ref获取数据
// 子组件和第二种方式一样的

// 父组件
<template>
  <div>
    <Student name="景天" :age="20" ref="student"></Student>
  </div>
</template>

<script>
import Student from './Student.vue'

export default {
  name: 'HelloWorld',
  data () {
    return {
    }
  },
  components: {
    Student
  },
  mounted() {
    this.getChildInfoOfRef()
  },
  methods: {
    getChildInfoOfRef() {
      console.log("这是ref获取数据", this.$refs.student.childName)
      this.$refs.student.$on('getchildrenInfo',this.getchildrenInfo)
    },
    getchildrenInfo(val) {
      console.log("这是子组件给我的数据", val)
    }
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值