Vue中的数据交互

父组件与子组件之间传值
  1. 父组件向子组件传值,通过prop传值
  • 组件中的代码
 <childComponent msg="hello" title="son" />
  • 子组件中的代码
 <p>{{msg}}--{{title}}<p/>
props:['msg','title']     //  props是跟data和methods等同级别的
  1. 子组件向父组件传值,通过$emit
  • 子组件代码
<button @click='possMsg'>子传父</button>
methods:{
    possMsg(){
      this.$emit('showMsg','hello father')    //  第一个参数是父组件中绑定到子组件的事件名,第二个参数是传入父组件的内容
    }
  }
  • 父组件的代码
<childComponent @showMsg='showMsg1' />
<p>{{msg}}</p>
data(){
	return{
		msg:''
	}
},
methods:{
    showMsg(val){
     this.msg=val
    }
  }
使用axios实现前后台数据交互
  1. 运行:npm i axios vue-axios -S 安装axios
  2. 在入口文件(main.js)引入axios和vue-axios
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
  1. 获取数据:按照下列三种方式
Vue.axios.get(api).then((response) => {
  console.log(response.data)
})
this.axios.get(api).then((response) => {
  console.log(response.data)
})
this.$http.get(api).then((response) => {
  console.log(response.data)
})
  1. 发送数据:
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
使用Vuex实现非父子组件之间传递
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值