Vue 父子組件、組件間通信
更新時(shí)間:2017年03月08日 15:58:38 作者:閣下長(zhǎng)的好生俊俏
本篇文章主要介紹了Vue 父子組件、組件間通信示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
本人對(duì)Vue組件間通信不懂,搜索了很多關(guān)于Vue 父子組件間通信介紹,下面我來記錄一下,有需要了解Vue 父子組件、組件間通信的朋友可參考。希望此文章對(duì)各位有所幫助。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>組件</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<script src="../js/Vue.js"></script>
<script>
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{},
components:{
aaa:{
template:'<h2>這是aaa組件<bbb></bbb></h2>',
components:{
bbb:{
template:'<h2>這是bbb組件</h2>'
}
}
}
}
});
}
</script>
</head>
<body>
<div id="box">
<aaa></aaa>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="../js/vue1.0.js"></script>
<script>
//子組件獲取父組件的值
window.onload = function () {
var vm = new Vue({
el: '#box',
data: {
a: 'aaa'
},
components: {
'aaa': {
data(){
return {
msg: 111,
msg2: '這是父組件的數(shù)據(jù)'
}
},
template: '#aaa',
components: {
'bbb': {
//html中的my-msg這種形式,js中應(yīng)該采用駝峰命名myMsg
props: ['mmm', 'myMsg'],
template: '<h3>我是bbb組件->{{mmm}}<br>{{myMsg}}</h3>'
}
}
}
}
});
}
</script>
</head>
<body>
<div id="box">
<aaa>
</aaa>
</div>
<template id="aaa">
<h1>11111</h1>
<bbb :mmm="msg2" :my-msg="msg"></bbb>
</template>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="../js/vue1.0.js"></script>
<script>
//子組件獲取父組件的值
window.onload = function () {
var vm = new Vue({
el: '#box',
data: {
a: 'aaa'
},
components: {
'aaa': {
data(){
return {
msg: 111,
msg2: '這是父組件的數(shù)據(jù)'
}
},
template: '#aaa',
components: {
'bbb': {
//html中的my-msg這種形式,js中應(yīng)該采用駝峰命名myMsg
props: ['mmm', 'myMsg'],
template: '<h3>我是bbb組件->{{mmm}}<br>{{myMsg}}</h3>'
}
}
}
}
});
}
</script>
</head>
<body>
<div id="box">
<aaa>
</aaa>
</div>
<template id="aaa">
<h1>11111</h1>
<bbb :mmm="msg2" :my-msg="msg"></bbb>
</template>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父組件獲取子組件的值</title>
<script src="../js/vue1.0.js"></script>
<script>
//父組件獲取子組件的值
window.onload = function () {
var vm = new Vue({
el:'#box',
data:{
a:'aaa'
},
components:{
'aaa':{
data(){
return {
msg:111,
msg2:'我是父組件的數(shù)據(jù)'
}
},
template:'#aaa',
methods:{
get(msg){
this.msg = msg;
}
},
components:{
'bbb':{
data(){
return {
a:'我是子組件的數(shù)據(jù)'
}
},
template:'#bbb',
methods:{
send(){
this.$emit('child-msg',this.a);
}
}
}
}
}
}
});
}
</script>
</head>
<body>
<div id="box">
<aaa></aaa>
</div>
<template id="aaa">
<span>我是父級(jí) -》 {{msg}}</span>
<bbb @child-msg="get"></bbb>
</template>
<template id="bbb">
<h3>子組件</h3>
<input type="button" value="send" @click="send" />
</template>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue子組件內(nèi)的props對(duì)象參數(shù)配置方法
這篇文章主要介紹了?Vue?子組件內(nèi)的??props?對(duì)象里的?default?參數(shù)是如何定義Array、?Object?、或?Function?默認(rèn)值的正確寫法說明,感興趣的朋友跟隨小編一起看看吧2022-08-08
vue如何在vue.config.js文件中導(dǎo)入模塊
這篇文章主要介紹了vue如何在vue.config.js文件中導(dǎo)入模塊問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
實(shí)用的 vue tags 創(chuàng)建緩存導(dǎo)航的過程實(shí)現(xiàn)
這篇文章主要介紹了實(shí)用的 vue tags 創(chuàng)建緩存導(dǎo)航的過程實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
vue監(jiān)聽瀏覽器的后退和刷新事件,阻止默認(rèn)的事件方式
這篇文章主要介紹了vue監(jiān)聽瀏覽器的后退和刷新事件,阻止默認(rèn)的事件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue中v-model指令與.sync修飾符的區(qū)別詳解
本文主要介紹了vue中v-model指令與.sync修飾符的區(qū)別詳解,詳細(xì)的介紹了兩個(gè)的用法和區(qū)別,感興趣的可以了解一下2021-08-08

