Vue ref属性、props属性、mixin属性

news/2024/7/10 0:40:20 标签: vue

文章目录

  • ref
  • props
  • $attrs
  • mixin

ref

ref 属性
1.被用来给元素或子组件注册引用信息((id的替代者)
2.应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)
3.使用方式:
打标识: <h1 ref="xxx">....</h1><School ref="xxx"></School>
获取: this.$refs.xxx

为了说明这个属性,我们重新写下相关代码:

main.js

//引入Vue
import Vue from 'vue';
//引入App
import App from './App';
//关闭vue的生产提示
Vue.config.productionTip = false
//创建vm
new Vue({
    el: "#app",
    render: h => h(App)
})

School.vue

<template>
  <div class="school">
    <h2>学校名称:{{ name }}</h2>
    <h2>学校地址:{{ address }}</h2>
  </div>
</template>

<script>
export default {
  name: "School",
  data() {
    return {
      name: "三里屯小学",
      address: "北京"
    }
  }
}
</script>

<style scoped>
.school {
  background-color: aliceblue;
}
</style>

App.vue

<template>
  <div>
    <h2 ref="title">欢迎学习Vue</h2>
    <button @click="showDom" ref="btn">点我输出上方dom</button>
    <School ref="sch"/>
  </div>
</template>

<script>
//引入School组件
import School from "@/components/School";

export default {
  name: "App",
  components: {
    School
  },
  methods:{
    showDom(){
      console.log(this.$refs);
      console.log(this.$refs.title);//真实Dom元素
      console.log(this.$refs.sch);//School组件的实例对象
    }
  }
}
</script>

<style scoped>
</style>

在这里插入图片描述

props

功能:让组件接收外部传过来的数据
(1).传递数据:
<Demo name="xxx" />
(2).接收数据:
第一种方式(只接收):

props: [ 'name']

第二种方式(限制类型):

props:{
	name : Number
}

第三种方式(限制类型、限制必要性、指定默认值):

props:{
	name:{
		type:String,//类型
		required:true,//必要性
		default:'老王’//默认值
	}
}

备注:props是只读的,Vue 底层会监测你对 props 的修改,如果进行了修改,就会发出警告,若业务需求确实需要修改,那么请复制 props 的内容到 data 中一份,然后去修改 data 中的数据

使用介绍
Student.vue

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

<script>
export default {
  name: "Student",
  data() {
    return {
      msg:"学生信息",
    }
  },
  //简单声明接受
  props:["name", "sex", "age"]
}
</script>

<style scoped>
</style>

App.vue

<template>
  <div>
    <Student name="张三" sex="" :age="18+1"/>
    <Student name="王老五" sex="" age="19"/>
  </div>
</template>

<script>
//引入School组件
import Student from "@/components/Student";

export default {
  name: "App",
  components: {
    Student
  }
}
</script>

<style scoped>
</style>

在这里插入图片描述
在接受时,还可以进行类型限制

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

这样在使用的时候,年龄只能传数值类型

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

或者写的更具体

//接收的同时对数据:类型限制+默认值指定+必要性限制
  props: {
    name: {
      type: String,//类型String
      required: true//必须传值
    },
    age: {
      type: Number,
      default: 99//默认99
    },
    sex: {
      type: String,
      required: true
    }
}

注意:传过来的值不能改变,如需改变,需要本地定义一个值

<template>
  <div>
    <h1>{{ msg }}</h1>
    <h2>学生姓名:{{ name }}</h2>
    <h2>学生性别:{{ sex }}</h2>
    <h2>学生年龄:{{ myAge }}</h2>
    <button @click="updateAge">点我修改传进来的年龄</button>
  </div>
</template>

<script>
export default {
  name: "Student",
  props: ["name", "sex", "age"],
  data() {
    return {
      msg: "学生信息",
      myAge: this.age
    }
  },
  methods: {
    updateAge() {
      console.log(this.myAge++);
    }
  }
}
</script>

<style scoped>
</style>

在这里插入图片描述

$attrs

App.vue 中引入并使用 Demo 组件,并向其传入一个 msg,值为 “hello ”

<template>
  <div>
    <Demo msg="hello"/>
  </div>
</template>

<script>
import Demo from './components/Demo'

export default {
  name: 'App',
  components: {
    Demo
  }
}
</script>

Demo 组件在 props 中接收,我们在 mounted 生命钩子中打印下 this

<template>
  <div>
    {{ msg }}
  </div>
</template>

<script>

export default {
  name: "Demo",
  props: ["msg"],
  mounted() {
    console.log(this);
  }
}
</script>

可以看到 vc 身上的 props 有刚才传过来的值
在这里插入图片描述
如果 Demo 中不使用 props 接收,也可以使用

<template>
  <div>
    {{ $attrs.msg }}
  </div>
</template>

<script>

export default {
  name: "Demo",

  mounted() {
    console.log(this);
  }
}
</script>

可以看到 vc 的 $attrs 上有刚才传过来的 msg,所以也可以使用 $attrs.msg使用传过来的值

mixin

功能:可以把多个组件共用的配置提取成一个混入对象。混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被“混合”进入该组件本身的选项
使用方式:
第一步、定义混合

{
	data(){....},
	methods:{....}
}

第二步、使用混入
(1).全局混入:Vue.mixin(xxx)
(2).局部混入:mixins: [ "xxx "]

新建 mixin.js

export const mixin = {
    methods: {
        alertName() {
            alert(this.name)
        }
    },
    mounted() {
        console.log("你好,mounted");
    }
}

export const mixin2 = {
    data() {
        return {
            x: 100,
            y: 200
        }
    }
}

Student.vue

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

<script>
//引入混合
import {mixin,mixin2} from "@/mixin";

export default {
  name: "Student",
  data() {
    return {
      name: "张三",
      sex: "男",
      x:300
    }
  },
  mounted() {
    console.log("hello mounted");
  },
  mixins: [mixin,mixin2]
}
</script>

<style scoped>
</style>

School.vue

<template>
  <div>
    <h2 @click="alertName">学校名称:{{ name }}</h2>
    <h2>学生地址:{{ address }}</h2>
  </div>
</template>

<script>
import {mixin} from "@/mixin";

export default {
  name: "School",
  data() {
    return {
      name: "三里屯大学",
      address: "北京"
    }
  },
  mixins: [mixin]
}
</script>

<style scoped>
</style>

App.vue

<template>
  <div>
    <Student/>
    <hr/>
    <School/>
  </div>
</template>

<script>
//引入School组件
import Student from "@/components/Student";
import School from "@/components/School";

export default {
  name: "App",
  components: {
    Student,
    School
  }
}
</script>

<style scoped>
</style>

在这里插入图片描述
以上是局部引入mixin,下面介绍全局引入 mixin,修改 main.js

//引入Vue
import Vue from 'vue';
//引入App
import App from './App';

import {mixin,mixin2} from "@/mixin";

//关闭vue的生产提示
Vue.config.productionTip = false
Vue.mixin(mixin)
Vue.mixin(mixin2)

//创建vm
new Vue({
    el: "#app",
    render: h => h(App)
})



http://www.niftyadmin.cn/n/1816750.html

相关文章

ActiveX控件的打包发布[无证书发布]

最近为了解决一个ActiveX的技术问题&#xff0c;用VB做了一个ActiveX控件&#xff0c;什么功能都没有&#xff0c; 就是测试一下ActiveX的发布&#xff0c;以及版本更新&#xff01;折腾了两天&#xff0c;总算搞明白其中的一些过程&#xff01; 顺便记一下&#xff0c;免得忘记…

光和时间的关系

实际上来说&#xff0c;时间是个概念&#xff0c;是人类提出来的&#xff0c;它并不存在于宇宙中&#xff0c;她是人类为了区分变化而提出来的概念&#xff0c;所以&#xff0c;实际存在的是“变化”。 根据爱因斯坦的相对论&#xff0c;光速是宇宙中最快的速度。也就是信号传递…

Vue TodoList案例

文章目录界面完成添加功能实现勾选实现方法一实现方法二删除底部统计功能实现底部清除已完成任务总结界面完成 components 文件夹下新建 Header.vue来完成头部组件 <template><div class"todo-header"><input type"text" placeholder"…

一张图看懂 JS 原型链

JS 原型链&#xff0c;画了张图&#xff0c;终于理清楚各种关系有木有 写在最后&#xff1a; __proto__是每个对象都有的一个属性&#xff0c;而prototype是函数才会有的属性!!! function Person() { } 是函数 var person new Person();  // person 是对象 https://www.…

[Asp.net]HyperLinkColumn應用2/28

<datagrid> ...<asp:HyperLinkColumn HeaderText"傳票單號" DataTextField"accno" DataNavigateUrlField"accno" DataNavigateUrlFormatString"../acc/summons.ASPx?id3106&id1{0}" Target"_blank">…

Vue 浏览器本地存储

文章目录localstorageSessionStorage总结TodoList 改为本地存储localstorage <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>浏览器本地存储</title> </head> <body> <div id&…

控制HTML页面内容不能选中的方法

方法有二 一&#xff1a; css 方法 user-seletct: none;-webkit-user-seletct: none;-moz-user-seletct: none;-ms-user-seletct: none; none: 不能选中内容 text: 能选中内容 二&#xff1a;js 方法 document.body.onselectstart function(){  return false;} 返回 …

VSS出现的是file name.dat may be corrupt错误

出现这个错误的时候&#xff0c;只要从命令窗口CD到VSS的安装目录下的win32目录中。 运行 "ANALYZE.EXE -X E:\VSS\data" (后面添对应的目录)。 便会找出对应的出错的文件。 再 “ANALYZE -F e:\vss\data” (需要所有的用户都退出VSS后才能执行)。 每次执行一条命…