使用vue脚手架(二)ref属性&props配置&mixin混入

1.ref属性

*被用来给元素或子组件注册引用信息

*应用在html标签上获取的是真实的dom元素,应用在组件标签上是组件实例对象(vc)

<template>
    <div>
        <!-- ref用来给元素或子组件注册引用信息 -->
        <h2 ref="title">{{msg}}</h2>
        <button v-on:click="showDom">点击输出上方dom</button>
        <button v-on:click="showDom2">点击输出下方元素</button>
        <school ref="title2"></school>
    </div>
</template>
<script>
    import school from './components/school'
    export default {
        name:"App",
        components:{
            school
        },
        data() {
            return {
                msg:"欢迎新同学"
            }
        },
        methods: {
            showDom(){
                console.log(this.$refs.title)
            },
            showDom2(){
                console.log(this.$refs.title2)
            }
        },
    }
</script>

2.props配置

*功能:让组件接收外部传过来的数据

*接收:

<template>
  <div>
      <h1>{{msg}}</h1>
      <h2>学生姓名:{{name}}</h2>
      <h2>学生年龄:{{age}}</h2>
      <h2>学生性别:{{sex}}</h2>
  </div>
</template>

<script>
export default {
    name:"Student",
    data() {
        return {
            msg:"我是一个学生"
        }
    },
    //props接收到的东西是不能更改的
    props:['name','sex','age']//简单接收

    // props:{
    //     name:String,
    //     age:Number,
    //     sex:String
    // }//接收的同时对数据进行类型限制

    // props:{
    //     name:{
    //         type:String,//类型
    //         required:true//必要的
    //     },
    //     age:{
    //         type:Number,
    //         default:23//默认值
    //     },
    //     sex:{
    //         type:String,
    //         required:true
    //     }
    // }//更多的限制
}
</script>

*传入:

<template>
  <Student name="张三" sex="女" :age='18'/>
</template>

<script>
import Student from './components/Student.vue'
export default {
    components:{
        Student
    },

}
</script>

3.mixin混入

*两个组件共享一个配置

1.写混入函数:

export const hunhe={
    methods: {
        showName(){
            alert(this.name)
        }
    }
}
export const num={
    data() {
        return {
            x:1,
            y:2
        }
    },
}

2.在main.js里引入混入:

import Vue from 'vue'
import App from './App.vue'
import {hunhe,num} from './mixin'
Vue.config.productionTip = false
Vue.mixin(hunhe,num)
new Vue({
    render: h => h(App),
  }).$mount('#app')

3.在组件中使用混入:

<template>
  <div>
      <h2 @click="showName">学生姓名:{{name}}</h2>
      <h2>学生性别:{{sex}}</h2>
  </div>
</template>

<script>
import {hunhe,num} from '../mixin'
export default {
    data() {
        return {
            name:"张三",
            sex:"男"
        }
    },
    mixins:[hunhe,num]
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桃桃tao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值