vue两个子组件如何相互通讯传值?
网友回复
父组件
创建子组件公用的空vue变量,为pubVue,并传给需要互相传参/互相调用方法的两个子组件
<template> <div> <btn-tools :pubVue="pubVue" /> <table-list :pubVue="pubVue" /> </div> </template> <script> // 组件引用 import TableList from './components/table-list'//组件一 import BtnTools from './components/btn-tools' //组件二 import Vue from 'vue' export default { name: 'PDmaterialList', components: { TableList, BtnTools }, data() { return { pubVu...
点击查看剩余70%
如果是cdn vue方式编写的代码,可以这样实现子组件之间的通讯及父子组件之间的通讯,代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" /> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue@2.6.1-dev.js"></script> <style> .comment-box{ padding: 5px 10px; border: 1px solid grey; margin:5px; } .reply{ color: red; font-weight: bold; } </style> </head> <body> <div id="app"> <div id="comment"> <Reply :commentdata="comments" v-on:replyit="replyit"></Reply> </div> <Comment ref="child" :towho="towho" v-on:sendit="sendit"></Comment> </div> <script type="text/javascript"> Vue.component('Comment',{ template: ' <form><input :placeholder="towho" /><input @click="sendit" type="button" value="回复" /></form>', props: ['towho'], data: function () { return { }; }, mounted: function () { }, methods: { sendit(){ this.$emit('sendit', "小王"); }, clean(){ alert("clear"); } } }); Vue.component('Reply',{ template: '<div><div class="comment-box" v-for="item in commentdata"><span>{{item.nickname}}说</span> <span>{{item.content}}</span> <span @click="replyit(item.id,item.nickname)" class="reply">回复</span><Reply v-bind="$attrs" v-on="$listeners" v-if="item.replyComments" :commentdata="item.replyComme...
点击查看剩余70%