目录

在不使用vuex的情况下,组件间传值的方式是通过父传子的方式或者兄弟组件传值。

父传子:

fathercomponent:

<template>
    <div>
        <helloworld :needdata="content"></helloworld>
    </div>
</template>

<script>
import helloworld from '../components/helloworld.vue'
export default {
    components:{
        helloworld
    },
    data(){
        return{
            content:"content"
        }
    }
}
</script>

<style lang="less" scoped>

</style>

soncomponent(子组件名称为helloworld):

<template>
    <div>
        <h1>helloworld</h1>
    </div>
</template>

<script>
export default {
    props:["needdata"],
    data(){
        return{
            h:this.needdata,
        }
    },
    mounted(){
        console.log(this.h);
    }
}
</script>

<style lang="less" scoped>

</style>

子传父:

fathercomponent:

<template>
    <div>
        <helloworld @senddata="getdata"></helloworld>
    </div>
</template>

<script>
import helloworld from '../components/helloworld.vue'
export default {
    components:{
        helloworld
    },
    data(){
        return{
            
        }
    },
    methods:{
        getdata(sondata){
            console.log("data=>",sondata);
        },
    }
}
</script>

<style lang="less" scoped>

</style>

soncomponent:

<template>
    <div>
        <h1>helloworld</h1>
    </div>
</template>

<script>
export default {
    data(){
        return{
            content:"content"
        }
    },
    mounted(){
        this.$emit("senddata",this.content);
    }
}
</script>

<style lang="less" scoped>

</style>

效果图:

实际上,为了数据能在父子组件间传值;还可以通过调用父组件的函数或调用子组件的函数的方式实现传值。 vue中子组件调用父组件的函数

vue父组件调用子组件的函数

vuex是vue框架中不可或缺的一部分;

vuex在需要多组件通信的时候显得格外重要;比如数据在父组件形成,但数据需要在子组件的子组件中使用时,就可以使用vuex管理;或者说需要兄弟组件传值时,可以使用vuex。

在vue的store.js中有五个属性:
分别是state,mutations,actions,getters,modules

结构为:

let a={
  state: {
  	name:"modulea"
  },
  //mutations专门用于改变state属性中的数据
  mutations: {
  	setfun(state,item){
		state.name=item;
	}
  }
}

export default new vuex.store({
  //state专门存放数据
  state: {
  	num:100,
  	useacomponent:{
		name:"a",
	},
	usebcomponent:"content",
  },
  //mutations专门用于改变state属性中的数据
  mutations: {
  	setstatefun(state,item){
		state.usebcomponent="bcomponent";
	}
  },
  actions: {
  	httpgetdata(store,item){
		settimeout(()=>{
			console.log(item);
			store.commit("setstatefun",item);
		},3000)
	}
  },
  getters:{
  //调用getters中的函数时没有入参
	getterfun1(state){
		return state.num++
	}
  //调用getters中的函数时有入参
  	gettterfun2(state){
		return function(val){
			return state.num+=val;
		}
	}
  },
  modules: {
  	modulea:a
  }
});
}

state中的数据可以在不同组件中访问获取。

获取state的数据:

this.$store.state.state对象中的数据;
例如
let val=this.$store.state.num;

更改state数据,就是调用vuex的mutations对象中的函数:

this.$store.commit("函数名","数据");
例如
this.$store.commit("setstatefun","testsetitem");

actions对象,用于在vuex中发请求

this.$store.dispatch("函数名","数据");
例如
this.$store.dispatch("httpgetdata","testitem");

getters对象,类似vue的计算属性

this.$store.getters.函数名;
例如
//没入参时
this.$store.getters.getterfun1;
//有入参时
this.$store.getters.getterfun2(123);

modules对象,类似将需要使用的store模块化分开,每个modules对象对应一个模块

//获取modules对象中的state数据
this.$store.state.modules对象名.state值;
例如
this.$store.state.modulea.name
//使用modules对象中的mutations的函数
this.$store.commit("函数名","入参数据");
例如
this.$store.commit("setfun","itemabc");
//这里需要注意,如果modules模块中与外部(不是modules对象模块)的mutations对象中有相同名字的函数时,则相同名字的函调用时都会执行

到此这篇关于浅谈vue的组件间传值(包括vuex)的文章就介绍到这了,更多相关vue 组件间传值内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!