vue組件通信傳值操作示例
本文實(shí)例講述了vue組件通信傳值操作。分享給大家供大家參考,具體如下:
父子組件通信:
子組件
<template> <div> <h3 @click="alerrt"> 我是子組件一</h3> <span>{{parentMessage}}</span> </div> </template> <script> export default{ props: ['parentMessage'], mounted() { // this.$emit('childEvent'); }, methods:{ alerrt(){ this.$emit('childEvent',{name:'zhangsan',age:10 }); } } } </script> <style scoped> </style>
父組件
<template> <div> <h2>父組件</h2> <span>父組件傳遞消息給子組件</span> <br> <router-view @childEvent="parentMethod" :parentMessage="parentMessage" /> <!-- <Child-one :parentMessage="parentMessage"></Child-one> --> <button type="" @click='extendTest'>extend</button> <div id="extend"></div> </div> </template> <script> import ChildOne from './ChildOne' export default{ components: { ChildOne }, methods: { parentMethod({name,age}) { alert(this.parentMessage); console.log(this.parentMessage,name,age); }, extendTest() { console.log('333'); var Extend = Vue.extend({ template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>', data: function () { return { firstName: 'Walter', lastName: 'White', alias: 'Heisenberg' } } }) new Extend().$mount('#extend') }, }, data () { return { parentMessage: '我是來自父組件的消息aaaa' } } } </script> <style scoped> </style>
兄弟組件通信:
在main,js里加
import Vue from 'vue' window.eventBus = new Vue();
在組件里
兄弟1組件:
window.eventBus.$emit('函數(shù)名稱', {參數(shù) 鍵:值});
兄弟2組件:
window.eventBus.$on('grouprecording',參數(shù) =>{ //處理數(shù)據(jù) })
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章
vue-cli系列之vue-cli-service整體架構(gòu)淺析
這篇文章主要介紹了vue-cli系列之vue-cli-service整體架構(gòu)淺析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01vue :style設(shè)置背景圖片方式backgroundImage
這篇文章主要介紹了vue :style設(shè)置背景圖片方式backgroundImage,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10解決Vue keep-alive 調(diào)用 $destory() 頁面不再被緩存的情況
這篇文章主要介紹了解決Vue keep-alive 調(diào)用 $destory() 頁面不再被緩存的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Vue3.3?+?TS4構(gòu)建實(shí)現(xiàn)ElementPlus功能的組件庫示例
Vue.js?是目前最盛行的前端框架之一,而?TypeScript?則是一種靜態(tài)類型言語,它能夠讓開發(fā)人員在編寫代碼時愈加平安和高效,本文將引見如何運(yùn)用?Vue.js?3.3?和?TypeScript?4?構(gòu)建一個自主打造媲美?ElementPlus?的組件庫2023-10-10Vue2.x Todo之自定義指令實(shí)現(xiàn)自動聚焦的方法
我們希望用戶雙擊 todo 進(jìn)入編輯狀態(tài)后輸入框自動獲取焦點(diǎn),而不是需要先手動點(diǎn)一下。這篇文章主要介紹了Vue 2.x Todo之自定義指令實(shí)現(xiàn)自動聚焦,非常具有實(shí)用價值,需要的朋友可以參考下2019-01-01vue .then和鏈?zhǔn)秸{(diào)用操作方法
這篇文章主要介紹了vue .then和鏈?zhǔn)秸{(diào)用操作方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07vue+vant-UI框架實(shí)現(xiàn)購物車的復(fù)選框全選和反選功能
這篇文章主要介紹了vue+vant-UI框架實(shí)現(xiàn)購物車的復(fù)選框全選和反選功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11