欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中的?ref,props,mixin屬性

 更新時間:2022年05月27日 08:49:41   作者:??奔跑吧雞翅????  
這篇文章主要介紹了Vue中的ref,props,mixin屬性,文章圍繞主題ref,props,mixin展開詳細(xì)內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

ref

ref 屬性:

  • 1.被用來給元素或子組件注冊引用信息((id的替代者)
  • 2.應(yīng)用在html標(biāo)簽上獲取的是真實(shí)DOM元素,應(yīng)用在組件標(biāo)簽上是組件實(shí)例對象(vc)
  • 3.使用方式: 打標(biāo)識: <h1 ref="xxx">....</h1><School ref="xxx"></School> 獲取: this.$refs.xxx

為了說明這個屬性,我們重新寫下相關(guān)代碼:

main.js

//引入Vue
import Vue from 'vue';
//引入App
import App from './App';
//關(guān)閉vue的生產(chǎn)提示
Vue.config.productionTip = false
//創(chuàng)建vm
new Vue({
    el: "#app",
    render: h => h(App)
})

School.vue

<template>
  <div class="school">
    <h2>學(xué)校名稱:{{ name }}</h2>
    <h2>學(xué)校地址:{{ address }}</h2>
  </div>
</template>

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

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

App.vue

<template>
  <div>
    <h2 ref="title">歡迎學(xué)習(xí)Vue</h2>
    <button @click="showDom" ref="btn">點(diǎn)我輸出上方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);//真實(shí)Dom元素
      console.log(this.$refs.sch);//School組件的實(shí)例對象
    }
  }
}
</script>

<style scoped>
</style>

props

功能:讓組件接收外部傳過來的數(shù)據(jù)

  • (1).傳遞數(shù)據(jù): <Demo name="xxx" /> 
  • (2).接收數(shù)據(jù):

第一種方式(只接收):

props: [ 'name']

第二種方式(限制類型):

props:{
	name : Number
}

第三種方式(限制類型、限制必要性、指定默認(rèn)值):

props:{
	name:{
		type:String,//類型
		required:true,//必要性
		default:'老王'//默認(rèn)值
	}
}

備注:props是只讀的,Vue 底層會監(jiān)測你對 props 的修改,如果進(jìn)行了修改,就會發(fā)出警告,若業(yè)務(wù)需求確實(shí)需要修改,那么請復(fù)制 props 的內(nèi)容到 data 中一份,然后去修改 data 中的數(shù)據(jù)

使用介紹 Student.vue

<template>
  <div>
    <h1>{{msg}}</h1>
    <h2>學(xué)生姓名:{{ name }}</h2>
    <h2>學(xué)生性別:{{ sex }}</h2>
    <h2>學(xué)生年齡:{{ age }}</h2>
  </div>
</template>

<script>
export default {
  name: "Student",
  data() {
    return {
      msg:"學(xué)生信息",
    }
  },
  //簡單聲明接受
  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>

在接受時,還可以進(jìn)行類型限制

 //接受的同時進(jìn)行類型限制
  props:{
    name:String,
    age:Number,
    sex:String
  }

這樣在使用的時候,年齡只能傳數(shù)值類型

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

或者寫的更具體

//接收的同時對數(shù)據(jù):類型限制+默認(rèn)值指定+必要性限制
  props: {
    name: {
      type: String,//類型String
      required: true//必須傳值
    },
    age: {
      type: Number,
      default: 99//默認(rèn)99
    },
    sex: {
      type: String,
      required: true
    }

注意:傳過來的值不能改變,如需改變,需要本地定義一個值

<template>
  <div>
    <h1>{{ msg }}</h1>
    <h2>學(xué)生姓名:{{ name }}</h2>
    <h2>學(xué)生性別:{{ sex }}</h2>
    <h2>學(xué)生年齡:{{ myAge }}</h2>
    <button @click="updateAge">點(diǎn)我修改傳進(jìn)來的年齡</button>
  </div>
</template>
<script>
export default {
  name: "Student",
  props: ["name", "sex", "age"],
  data() {
    return {
      msg: "學(xué)生信息",
      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

功能:可以把多個組件共用的配置提取成一個混入對象 使用方式: 第一步、定義混合

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

第二步、使用混入

  • (1).全局混入:Vue.mixin(xxx)
  • (2).局部混入:mixins: [ "xxx "]

Student.vue

<template>
  <div>
    <h2 @click="alertName">學(xué)生姓名:{{ name }}</h2>
    <h2>學(xué)生性別:{{ 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">學(xué)校名稱:{{ name }}</h2>
    <h2>學(xué)生地址:{{ address }}</h2>
  </div>
</template>
<script>
import {mixin} from "@/mixin";
export default {
  name: "School",
  data() {
    return {
      name: "三里屯大學(xué)",
      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.js

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

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

以上是局部引入mixin,下面介紹全局引入 mixin,修改 main.js

//引入Vue
import Vue from 'vue';
//引入App
import App from './App';
import {mixin,mixin2} from "@/mixin";
//關(guān)閉vue的生產(chǎn)提示
Vue.config.productionTip = false
Vue.mixin(mixin)
Vue.mixin(mixin2)
//創(chuàng)建vm
new Vue({
    el: "#app",
    render: h => h(App)
})

到此這篇關(guān)于Vue中的 ref,props,mixin屬性的文章就介紹到這了,更多相關(guān)Vue 屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論