美文网首页前端学习笔记
vue的父子传值和使用vuex兄弟传值

vue的父子传值和使用vuex兄弟传值

作者: 简小咖 | 来源:发表于2018-06-21 10:26 被阅读122次

在父组件中引入子组件

<template>
     <child-component></child-component>
</template>
<script>
import childComponent from './components/childComponent'

export default {
  name: 'parentComponent',
  components: {
    childComponent
  }
}
</script>

父传子

父组件

<template>
<child-component :sendList="pList" ></child-component>
</template>
<script>
import childComponent from './components/childComponent'
export default {
  name: 'parentComponent',
  components: {
    childComponent
  },
  data() {
    return {
      pList: []
    }
  },
  methods: {
    getList() {
      getListAxio().then(response => {
        this.pList = response.data
      })
    },
  }
}
</script>

子组件

<template>
<div class="nodata" v-if="sendList.length<=0">
      暂无数据
    </div>
    <div v-else>
        <div v-for="(item,index) in sendList" :key="index" >
          {{item}}
        </div>
    </div>
</template>
<script>
export default {
  props: ['sendList']
}
</script>

子传父

子组件

<template>
   <el-button @click="sendMsg" size="small" type="primary">确定</el-button>
</template>
<script>
export default {
  data() {
    return {
      aaa: '111'
    }
  },
methods: {
    sendMsg() {
      this.$emit('sendMessage', this.aaa)
    }
 }
}
</script>

父组件

<template>
<child-component  @sendMessage="message"></child-component>
</template>
<script>
import childComponent from './components/childComponent'
export default {
  name: 'parentComponent',
  components: {
    childComponent
  },
  data() {
    return {
      bbb:''
    }
  },
  methods: {
    message(val) {
      this.bbb = val    // 111
    },
  }
}
</script>

兄弟传值

兄弟之间可以通过vue官网的方法,在这里介绍一下vuex的方法

  • 建立store


    image.png

    index.js

import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import getters from './getters'
Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    user,
  },
  getters
})

export default store

user.js

const system = {
  state: {
    list: '',
    updateStatus: false,
  },

  mutations: {
    SET_LIST: (state, sendlist) => {
      state.list = sendlist
    },
    UPDATE_STATUS: (state, sendupdateStatus) => {
      state.updateStatus = sendupdateStatus
    },
  }
}
export default user

getters.js

const getters = {
  // app首页管理
  list: state => state.user.list,
  updateStatus: state => state.user.updateStatus
}
export default getters

发送方

<script>
export default {
  data() {
    return {
      myList:[]
    }
  },

  methods: {
  // 预览banner
    getMyList() {
      getList().then(response => {
        this.myList = response.data
        this.$store.commit('SET_LIST', this.myList)
        this.$store.commit('UPDATE_STATUS', true)
      })
    },
  }
}
</script>

接收方

<script>
import { mapGetters } from 'vuex'
  computed: {
    ...mapGetters([
      'list',
      'updateStatus'
    ])
  },
watch: {    //通过监听watch,可以根据接收数据的变化做相应的操作
    updateStatus() {
      if (this.updateStatus) {
        alert(1)
      }
    },
    list: {     //深度监听
      handler(newValue, oldValue) {
        console.log(newValue)
      },
      deep: true
    }
  },
}
</script>

相关文章

  • Vue组件之间的传值

    Vue父子组件之间的传值(props)兄弟组件 VUEX

  • 2020-03月前端面试题

    vue相关 vue父子组件传值方式有哪些? 兄弟组件间如何传值? vuex是用来干什么的? vuex核心模块有哪些...

  • 组件通信

    vue传值可分为父子之间传值、兄弟组件之间传值、跨代组件之间传值 1.父子之间传值:可以使用$emit/props...

  • vuex最详细完整的使用用法

    为什么使用vuex? vuex主要是是做数据交互,父子组件传值可以很容易办到,但是兄弟组件间传值(兄弟组件下又有父...

  • vue的父子传值和使用vuex兄弟传值

    在父组件中引入子组件 父传子 父组件 子组件 子传父 子组件 父组件 兄弟传值 兄弟之间可以通过vue官网的方法,...

  • vue组件间传值之eventBus

    1 概述: vue组件间的传值,父子之间props 和emit; 跨组件间可以使用vuex或者eventBus; ...

  • 组件之间的传值

    组件之间的传值,包括父子组件传值,兄弟组件之间的传值,其中父子组件包括父组件向子组件传值和子组件向父组件传值,现在...

  • vuex的基本概念

    一、什么时候使用Vuex 如果应用比较简单,就不需要使用Vuex,直接使用父子组件传值及及它传值方式即可,使用Vu...

  • vuex

    一、什么时候使用Vuex 如果应用比较简单,就不需要使用Vuex,直接使用父子组件传值及及它传值方式即可,使用Vu...

  • Vue父子组件通信和双向绑定

    本篇文章主要介绍父子组件传值,组件的数据双向绑定。 1. 基础父子组件传值 父子组件传值,这是Vue组件传值最常见...

网友评论

    本文标题:vue的父子传值和使用vuex兄弟传值

    本文链接:https://www.haomeiwen.com/subject/gxiheftx.html