Describes several methods for cross-component communication between vue and vue.

Source: Internet
Author: User

Describes several methods for cross-component communication between vue and vue.

When developing components, you will certainly encounter component communication. For example, you can click an icon to display a pop-up window and a masked layer. These three components are different. Managing the status between them becomes a problem.

Props bidirectional binding

Through sync bidirectional binding, attribute changes are synchronized to all components, which is also the simplest implementation method. The disadvantage is that there are many attributes. The implementation method is as follows:

App. vue File

<template> <div id="app">  <mask :hide-mask.sync="hideMask"></mask>  <dialog :hide-dialog.sync="hideDialog" :hide-mask.sync="hideMask"></dialog>  <dialog-icon :hide-dialog.sync="hideDialog" :hide-mask.sync="hideMask"></dialog-icon> </div></template><script>import mask from './components/mask/index'import dialog from './components/dialog/index'import dialogIcon from './components/dialog-icon/index'export default { components: {  mask,  dialog,  dialogIcon }, data () {  return {   hideMask: true,   hideDialog: true  } }}</script>

Component/dialog/index. vue File

<template> <section class="dialog" :class="{ 'hide': hideDialog }">  <div class="dialog-close" @click="hide()"></div> </section></template><script>export default { props: ['hideDialog', 'hideMask'], methods: {  hide () {   this.hideDialog = !this.hideDialog   this.hideMask = !this.hideMask  } }}</script>

Component/dialog-icon/index. vue File

<Template> <section class = "dialog-icon" @ click = "show ()"> click the displayed window </section> </template> <script> export default {props: ['hidemode', 'hidemask'], methods: {show () {this. hideDialog =! This. hideDialog this. hideMask =! This. hideMask }}</script>

Component/mask/index. vue File

<template> <div class="mask" :class="{ 'hide': hideMask }"></div></template><script>export default { props: ['hideMask']}</script>

Custom events

The child component $ dispatch () distributes events to the parent component, and the parent component $ broadcast () broadcasts events to the child component. Although this method reduces props usage, however, several additional events need to be defined, and more States will become complicated. The implementation method is as follows:

App. vue File

<template> <div id="app">  <mask></mask>  <dialog></dialog>  <dialog-icon></dialog-icon></template><script>import mask from './components/mask/index'import dialog from './components/dialog/index'import dialogIcon from './components/dialog-icon/index'export default { components: {  mask,  dialog,  dialogIcon }, data () {  return {   hideMask: true,   hideDialog: true  } }, events: {  'dialog-dispatch' () {   this.hidedialog = !this.hidedialog   this.$broadcast('dialog-broadcast')  },  'mask-dispatch' () {   this.hideMask = !this.hideMask   this.$broadcast('mask-broadcast')  } }}</script>

Component/dialog-icon/index. vue File

<Template> <section class = "dialog-icon" @ click = "show ()"> click the displayed window </section> </template> <script> export default {methods: {show () {this. $ dispatch ('Dialog-dispatch ') this. $ dispatch ('mask-dispatch ') }}, events: {'Dialog-broadcast' () {this. hideDialog =! This. hideDialog }}, data () {return {hideDialog: this. $ parent. hideDialog, hideMask: this. $ parent. hideMask }}</script>

Component/dialog/index. vue File

<template> <section class="dialog" :class="{ 'hide': hideDialog }">  <div class="dialog-close" @click="hide()"></div> </section></template><script>export default { methods: {  hide () {   this.$dispatch('dialog-dispatch')   this.$dispatch('mask-dispatch')  } }, events: {  'dialog-broadcast' () {   this.hideDialog = !this.hideDialog  } }, data () {  return {   hideDialog: this.$parent.hideDialog,   hideMask: this.$parent.hideMask  } }}</script>

Component/mask/index. vue File

<template> <div class="mask" :class="{ 'hide': hideMask }"></div></template><script>export default { data () {  return {   hideMask: this.$parent.hideMask  } }, events: {  'mask-broadcast' () {   this.hideMask = !this.hideMask  } }}</script>

Vuex

The status is centrally stored in the store for management, and the status is changed through mutations. The component calls mutations through action. Although this is a bit difficult, it will be better maintained after all things are put together. The implementation method is as follows:

App. vue File

<template> <div id="app">  <mask></mask>  <dialog></dialog>  <dialog-icon></dialog-icon> </div></template><script>import mask from './components/mask/index'import dialog from './components/dialog/index'import dialogIcon from './components/dialog-icon/index'export default { components: {  mask,  dialog,  dialogIcon }}</script>

Component/dialog/index. vue File

<template> <section class="storehouse dialog" :class="{ 'hide': isHideDialog }">  <div class="dialog-close" @click="hideDialog()"></div> </section></template><script>import { hideDialog } from '../../vuex/actions'export default { vuex: {  state: {   isHideDialog: state => state.isHideDialog  },  actions: {   hideDialog  } }}</script>

Component/dialog-icon/index. vue File

<Template> <section class = "storehouse-icon" @ click = "hideDialog () "> click the pop-up window </section> </template> <script> import {hideDialog} from '.. /.. /vuex/actions 'export default {vuex: {actions: {hideDialog }}</script>

Component/mask/index. vue File

<template> <div class="mask" :class="{ 'hide': isHideMask }"></div></template><script>export default { vuex: {  state: {   isHideMask: state => state.isHideMask  } }}</script>

Vuex/store. js File

import Vue from 'vue'import Vuex from 'vuex'import mutations from './mutations'Vue.use(Vuex)const state = { isHideMask: true, isHideDialog: true}const store = new Vuex.Store({ state, mutations})if (module.hot) { module.hot.accept(['./mutations'], () => {  const mutations = require('./mutations').default  store.hotUpdate({   mutations  }) })}export default store

Vuex/mutations. js File

import { HIDEDIALOG}from './mutation-types'exportdefault { [HIDEDIALOG] (state) {  state.isHideDialog = !state.isHideDialog  state.isHideMask = !state.isHideMask }}

Vuex/mutations-types.js files

export const HIDEDIALOG = 'HIDEDIALOG'

Vuex/action. js File

import { HIDEDIALOG } from './mutation-types'export const hideDialog = ({ dispatch }) => dispatch(HIDEDIALOG)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.