vue子組件如何使用父組件傳過(guò)來(lái)的值
更新時(shí)間:2022年04月08日 11:29:09 作者:我是小路路呀
這篇文章主要介紹了vue子組件如何使用父組件傳過(guò)來(lái)的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
子組件使用父組件傳過(guò)來(lái)的值
父組件
<alarmstatistics :roless.sync="role"></alarmstatistics>
??import alarmstatistics from "./alarmstatistics.vue";
? components: {
? ? alarmstatistics,
? },子組件
? props: ["roless"],
??
? data() {
? ? return {
? ? ? role:this.roless,
? },
??
? mounted() {
? ? if(this.role==3){
? ? ? this.chartY = "9.5%";
? ? }else{
? ? ? this.chartY = "18%";
? ? }
? },如果是使用父組件接口返回來(lái)的值,在html中直接使用
? props: ["infoDatac"],
? ? ? <li
? ? ? ? v-for="(item,i) in infoDatac.notice.admitted"
? ? ? ? :key="'A'+ i"
? ? ? >
? ? ? ? <div>申請(qǐng)單號(hào):{{ item.applyCode }}</div>
? ? ? ? <div>使用時(shí)間:{{ item.useTime }}</div>
? ? ? ? <span>{{ item.title }}</span>
? ? ? </li>vue子組件調(diào)用父組件數(shù)據(jù)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="" id="myVue">
<my-component>
</my-component>
</div>
<!--子組件-->
<template id="child" >
<div id="">
<div @click='changedata'>子組件:{{data}}</div>
</div>
</template>
<!--父組件-->
<template id="father">
<div>
<mycomponent-child v-bind:data="str"></mycomponent-child>
</div>
</template>
</body>
<script type="text/javascript" charset="utf-8">
/*在父組件中的數(shù)據(jù)str,
* 將父組件的數(shù)據(jù)綁定到子組件的屬性data上
* 然后在子組件中就可以通過(guò)props接收到,
* 這樣在子組件中就可以使用變量 this.data1訪問(wèn)到 父組件的 str1對(duì)應(yīng)的值了。
*/
//當(dāng)點(diǎn)擊子組件,觸發(fā)子組件的changedata方法,通過(guò)this.data = "父組件值被子組件修改了";改變了父級(jí)的str的值
//通過(guò) this.$parent.fn()訪問(wèn)父組件的方法fn()。
var child={
props:["data"],
template:"#child",
data:function(){
return{
str:"我是子組件數(shù)據(jù)"
}
},
methods:{
changedata:function(){
this.data = "父組件值被子組件修改了";
this.$parent.fn();
}
}
}
/*父組件*/
var father={
template:"#father",
data:function(){
return{
str:"我是父組件數(shù)據(jù)"
}
},
methods:{
fn:function(){
alert("我是父組件方法")
}
},
components:{
"mycomponentChild":child
}
}
vm=new Vue({
//el:"#myVue",
components:{
"myComponent":father
}
}).$mount('#myVue');
</script>
</html>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中typescript裝飾器的使用方法超實(shí)用教程
這篇文章主要介紹了vue中使用typescript裝飾器的使用方法超實(shí)用教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Vue簡(jiǎn)單封裝axios之解決post請(qǐng)求后端接收不到參數(shù)問(wèn)題
這篇文章主要介紹了Vue簡(jiǎn)單封裝axios之解決post請(qǐng)求后端接收不到參數(shù)問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
element日期選擇器el-date-picker樣式圖文詳解
最近寫的項(xiàng)目里面有一個(gè)功能是日期選擇功能,第一反應(yīng)是使用element里面的el-date-picker組件,下面這篇文章主要給大家介紹了關(guān)于element日期選擇器el-date-picker樣式的相關(guān)資料,需要的朋友可以參考下2022-09-09
如何使用 Vue Router 的 meta 屬性實(shí)現(xiàn)多種功能
在Vue.js中,Vue Router 提供了強(qiáng)大的路由管理功能,通過(guò)meta屬性,我們可以在路由定義中添加自定義元數(shù)據(jù),以實(shí)現(xiàn)訪問(wèn)控制、頁(yè)面標(biāo)題設(shè)置、角色權(quán)限管理、頁(yè)面過(guò)渡效果,本文將總結(jié)如何使用 meta 屬性來(lái)實(shí)現(xiàn)這些常見的功能,感興趣的朋友一起看看吧2024-06-06
Vue項(xiàng)目引發(fā)的「過(guò)濾器」使用教程
這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目引發(fā)的「過(guò)濾器」使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Vue前端如何設(shè)置Cookie和鑒權(quán)問(wèn)題詳解
這篇文章主要介紹了前端如何設(shè)置和使用Cookie,并對(duì)比了Cookie和Token在鑒權(quán)中的優(yōu)缺點(diǎn),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02

