vue基本知识问题(3)(一天一个问题26)

11.vuex

四个主要属性:state;mutations;getters;actions
获取:this. s t o r e . s t a t e . n a m e ; t h i s . store.state.name;this. store.state.namethis.store.getters.getAge
修改:this. s t o r e . c o m m i t ( ′ c h a n g e ′ , 10 ) ; t h i s . store.commit('change',10);this. store.commit(change,10);this.store.dispatch(‘add’, 5);

const store = new Vuex.Store({
		state:{age:10,name:1},   
		mutations:{									  
			change:function(state,a){
				return state.name += a;
			},
			changeAsync:function(state,a){
                console.log(state.num +=a);
            }
		},
		getters:{
			getAge:function(state){                   
                 return state.age;
            }
		},
		actions:{									
			add:function(context,value){			  
                setTimeout(function(){
           //提交事件
                    context.commit('changeAsync',value);
                },1000)   
            }
		}
	})

12.vue里面数据传递(prop,$emit,ref,vuex,eventBus)

12.1-父子组件(props,$emit)

props

//父组件
<v-head :childmsg="msg"></v-head>
data: function() {
	return {
		msg:"父主键的信息"
	}
},
//子组件
props: {
	childmsg: {
		type: String,
		default: "" 
	}
},
data: function (){
	return {
	}
};
watch:{
  'childmsg':function (newVal, oldVal) {
		
  }
}

$emit

//父组件
<div id="app">
    <!-- 事件的绑定 -->
    <com1 @func="show"></com1>
</div>
//子组件
methods:{
    myclick(){
        // 触发父组件的show方法
        this.$emit('func',this.sonmsg)
    }
}

ref

<v-home ref="home"></v-home>
//父组件拿子组件
this.$refs.home.属性
this.$refs.home.方法
//子组件拿父组件
this.$parent.数据
this.$parent.方法

12.2兄弟组件(vuex,eventBus)

//入口文件(main.js)
Vue.prototype.$bus = new Vue();
window.eventBus = new Vue();
//发送
Var data = “发送的信息”
eventBus.$emit('Jurisdiction', data);
//接收
eventBus.$on('Jurisdiction', function(val) {
    console.log(val)
});
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值