目录
1.被用来给元素或子组件注册引用信息(id的替代者)
2.应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)
3.使用方式:
打标识:
获取:this.$refs.xxx
具体看下图代码

功能:让组件接受外部传过来的数据
备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,
若业务需求确实需要修改,name请复制props的内容到data中一份,然后去修改data中的数据,具体做法查看下面的案例
<demo name="xxx">
props:['name','sex','age']
- props:{
- name: String,
- age:Number,
- sex:String
- }
- props:{
- name:{
- type:String,
- // 必须传入值
- required:true
- },
- age:{
- type:Number,
- // 如果不传入值的话,会有一个默认值
- default:99
- },
- sex:{
- type:String,
- required:true
- }
- }
student.vue
-
- <template>
-
- <div class="demo">
- <h2> 学生名称:{{name}}h2>
- <h2> 学生年龄:{{age}}h2>
- <h2> 学生性别:{{sex}}h2>
- <button @click="showName">点我提示学生名button>
- div>
- template>
-
- <script>
- export default {
- name:'Student',
- data(){
-
- return{
- // name:'张三',
- // sex:'男',
- // age:18
-
- //this.name是外部传进来的 this在这里值得vc
- // 这样也说明了props的优先级更高,要不然这里不能调用
- MyName:this.name
- }
- },
- methods: {
- showName(){
- alert(this.name)
- }
- },
- // 顺序不一定要对上
- // 接收外部传入数据
- // props:['name','sex','age'] //最简单的写法,但是不能限制传进来的数据
-
- // 接收的同时对数据进行类型限制
- // props:{
- // name: String,
- // age:Number,
- // sex:String
- // }
-
- // 接收的同时对数据进行类型限制+默认值的指定+必要性的限制
- props:{
- name:{
- type:String,
- // 必须传入值
- required:true
- },
- age:{
- type:Number,
- // 如果不传入值的话,会有一个默认值
- default:99
- },
- sex:{
- type:String,
- required:true
- }
- }
- }
- script>
-
- <style>
- /* 组件的样式 */
- .demo{
- background-color:orange
- }
-
- style>
-
-
-
-
-
-
app.vue
-
- <template>
- <div>
-
- <Student name="李四" sex="女" :age="18">Student>
- div>
-
- template>
-
- <script>
- // 引入组件
-
- import Student from './components/Student'
-
- export default {
- name:'App',
- // 注册组件
- components:{
- Student
- }
- }
- script>
-
- <style>
-
- style>

两个组件共享一个配置,并且这个配置要一个样
混合说白了就是共用配置


student.vue
-
- <template>
-
- <div class="demo">
- <h2 @click="showName"> 学生名称:{{name}}h2>
- <h2> 学生年龄:{{age}}h2>
- <h2> 学生性别:{{sex}}h2>
-
- div>
- template>
-
- <script>
- //引入一个混合 花括号中的名字是我们在minin中定义的
- import {mixin} from '../mixin'
-
- export default {
- name:'Student',
-
- data(){
- return{
- name:'张三',
- sex:'男',
- age:18
- }
- },
-
- // methods: {
- // showName(){
- // alert(this.name)
- // }
- // },
- mixins:[mixin]
- }
- script>
-
- <style>
- /* 组件的样式 */
- .demo{
- background-color:orange
- }
-
- style>
-
-
-
-
School.vue
-
- <template>
-
- <div class="demo">
- <h2 @click="showName"> 学校名称:{{name}}h2>
- <h2> 学校地址:{{address}}h2>
-
- div>
- template>
-
- <script>
- //引入一个混合 花括号中的名字是我们在minin中定义的
- import {mixin} from '../mixin'
-
- export default {
- name:'School',
- data(){
- return{
- name:'尚硅谷',
- address:'北京昌平',
- }
- },
- // 我们不再使用这种方式,我们使用混合
- // methods: {
- // showName(){
- // alert(this.name)
- // }
- // },
- mixins:[mixin]
-
-
- }
- script>
-
- <style>
- /* 组件的样式 */
- .demo{
- background-color:orange
- }
-
- style>
App.vue
-
- <template>
- <div>
- <school>school>
- <Student >Student>
- div>
-
- template>
-
- <script>
- // 引入组件
-
- import Student from './components/Student'
- import School from './components/School'
-
-
- export default {
- name:'App',
- // 注册组件
- components:{
- Student,
- School
- }
- }
- script>
-
- <style>
-
- style>
- export const mixin={
- methods:{
- showName(){
- alert(this.name)
- }
- }
- }


将上面的局部删掉,保留mixin.js文件
- // 该文件是整个项目的入口文件
-
- // 引入vue,这个vue不能解析template配置项
- import Vue from 'vue'
- // 下面这个是引入完整版的vue。这个vue能解析template配置项
- // import Vue from 'vue/dis/vue'
-
- // 引入APP组件,它是所有组件的父组件
- import App from './App.vue'
-
- // 引入混合
- import {mixin} from './mixin'
- Vue.mixin(mixin) //这样一来所有的vc和vm都会得到这个混合
- // Vue.mixin(mixin2) 有几个,就写一个这样的语句
-
- // 关闭vue的生产提示
- Vue.config.productionTip = false
-
- // 创建vue实例对象---vm
- new Vue({
- el:'#app',
- render: h => h(App),
- })
功能:增强Vue
本质:包含install方法的一个对象,install的第一个参数是vue,第二个以后的参数是插件使用者传递数据

定义插件:
对象.install = function(Vue,options){
全局过滤器
全局指令
配置全局混入
添加实例方法

}
使用插件:Vue.use()
编写插件

引入插件

调用插件

作用:让样式在局部生效,防止冲突
写法: