最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue中的?ref,props,mixin屬性

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

ref

ref 屬性:

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

為了說明這個(gè)屬性,我們重新寫下相關(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í)例對(duì)象
    }
  }
}
</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 底層會(huì)監(jiān)測(cè)你對(duì) props 的修改,如果進(jìn)行了修改,就會(huì)發(fā)出警告,若業(yè)務(wù)需求確實(shí)需要修改,那么請(qǐng)復(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é)生信息",
    }
  },
  //簡(jiǎn)單聲明接受
  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>

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

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

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

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

或者寫的更具體

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

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

<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 組件,并向其傳入一個(gè) msg,值為 “hello ”

<template>
  <div>
    <Demo msg="hello"/>
  </div>
</template>
<script>
import Demo from './components/Demo'
export default {
  name: 'App',
  components: {
    Demo
  }
}
</script>

Demo 組件在 props 中接收,我們?cè)?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

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

{
	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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • element step組件在另一側(cè)加時(shí)間軸顯示

    element step組件在另一側(cè)加時(shí)間軸顯示

    本文主要介紹了element step組件在另一側(cè)加時(shí)間軸顯示,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • vue項(xiàng)目中使用axios遇到的相對(duì)路徑和絕對(duì)路徑問題

    vue項(xiàng)目中使用axios遇到的相對(duì)路徑和絕對(duì)路徑問題

    這篇文章主要介紹了vue項(xiàng)目中使用axios遇到的相對(duì)路徑和絕對(duì)路徑問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 在vue項(xiàng)目中使用sass語法問題

    在vue項(xiàng)目中使用sass語法問題

    sass是一個(gè)最初由Hampton Catlin設(shè)計(jì)并由Natalie Weizenbaum開發(fā)的層疊樣式表語言。這篇文章主要介紹了在vue項(xiàng)目中使用sass語法,需要的朋友可以參考下
    2019-07-07
  • vue項(xiàng)目中添加單元測(cè)試的方法

    vue項(xiàng)目中添加單元測(cè)試的方法

    這篇文章主要介紹了vue項(xiàng)目中添加單元測(cè)試的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • 前端報(bào)錯(cuò)npm ERR! cb() never called!問題解決辦法

    前端報(bào)錯(cuò)npm ERR! cb() never called!問題解決辦法

    最近接手了一個(gè)前臺(tái)項(xiàng)目,執(zhí)行npm install的時(shí)候一直報(bào)錯(cuò),所以這里就給大家總結(jié)下,這篇文章主要給給大家介紹了關(guān)于前端報(bào)錯(cuò)npm?ERR! cb() never called!問題的解決辦法,需要的朋友可以參考下
    2024-05-05
  • 一文解決vue2 element el-table自適應(yīng)高度問題

    一文解決vue2 element el-table自適應(yīng)高度問題

    在寫公司后臺(tái)項(xiàng)目的時(shí)候遇到一個(gè)需求,要求表格頁面不能有滾動(dòng)條,所以必須封裝一個(gè)公共方法來實(shí)現(xiàn)表格自適應(yīng)高度,本問小編給大家介紹了如何解決vue2 element el-table自適應(yīng)高度問題,需要的朋友可以參考下
    2023-11-11
  • 基于Vue方法實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器

    基于Vue方法實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器

    這篇文章主要為大家詳細(xì)介紹了基于Vue方法實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Vue中圖片上傳組件封裝-antd的a-upload二次封裝的實(shí)例

    Vue中圖片上傳組件封裝-antd的a-upload二次封裝的實(shí)例

    這篇文章主要介紹了Vue中圖片上傳組件封裝-antd的a-upload二次封裝的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue模態(tài)框?qū)崿F(xiàn)動(dòng)態(tài)錨點(diǎn)

    vue模態(tài)框?qū)崿F(xiàn)動(dòng)態(tài)錨點(diǎn)

    這篇文章主要為大家詳細(xì)介紹了vue模態(tài)框?qū)崿F(xiàn)動(dòng)態(tài)錨點(diǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • vue?watch監(jiān)聽方法總結(jié)

    vue?watch監(jiān)聽方法總結(jié)

    這篇文章主要給大家分享的是vue?watch監(jiān)聽方法總結(jié),偵聽器一般來說是用來監(jiān)聽數(shù)據(jù)的變化,默認(rèn)是在數(shù)據(jù)發(fā)生變化時(shí)執(zhí)行。監(jiān)聽的數(shù)據(jù)名放到這里面作為函數(shù)名,這個(gè)函數(shù)里面有兩個(gè)參數(shù),一個(gè)是新值,一個(gè)是舊值。下面我們就一起進(jìn)入文章了解更具體的內(nèi)容吧
    2021-12-12

最新評(píng)論

友谊县| 乐陵市| 合阳县| 富源县| 苏尼特右旗| 南木林县| 定襄县| 罗城| 台南县| 温泉县| 镇江市| 吉木萨尔县| 浙江省| 禄劝| 平邑县| 大洼县| 论坛| 金昌市| 颍上县| 溧阳市| 若尔盖县| 晋宁县| 饶平县| 马关县| 富平县| 县级市| 关岭| 彭泽县| 珠海市| 工布江达县| 巨野县| 邯郸县| 山东| 微博| 铅山县| 高陵县| 永春县| 昌黎县| 玉溪市| 通城县| 桂平市|