Vue学习之旅----父子组件传值

父传子  props

<template>
  <div>Home
    <button @click="getHomeData()">Home请求数据</button>
    <ul>
      <li v-for="(item,key) in list" :key=key>
        {{item.title}}
      </li>
    </ul>
    <!-- 给子组件传值绑定title属性 -->
    <v-header :title='title'></v-header>
    <v-life v-if="flag"></v-life>
    <button @click="flag=!flag">挂载和卸载组件</button>
  </div>
</template>
<script>
import Header from './Header.vue'
import Life from './Life.vue'
import Axios from 'axios'
export default {
  data () {
    return {
      msg: 'home',
      flag: true,
      list: [],
      title: '父组件要传给子组件的值'
    }
  },
  components: {
    'v-header': Header,
    'v-life': Life
  },
  methods: {
    getHomeData () {
      // 请求数据
      var api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1'
      Axios.get(api).then((response) => {
        this.list = response.data.result
      }).catch((error) => {
        console.log(error)
      })
    }
  }
}
</script>
<style lang="css" scope>
</style>
<template>
  <div>
    <h2>头部组件--{{msg}}--{{title}}</h2>
  </div>
</template>
<script>
export default {
  data () {
    return {
      msg: '子组件自身属性'
    }
  },
  methods: {

  },
  // 接受父组件传过来的值
  props: ['title']
}
</script>
<style>
</style>

父组件也可以给子组件传递方法或者自身,同时,子组件执行父组件的方法,通过传参,实现子组件给父组件传值

<template>
  <div>Home
    <button @click="getHomeData()">Home请求数据</button>
    <ul>
      <li v-for="(item,key) in list" :key=key>
        {{item.title}}
      </li>
    </ul>
    <!-- 给子组件传值绑定title属性 -->
    <v-header :title='title' :run="run" :home="this"></v-header>
    <v-life v-if="flag"></v-life>
    <button @click="flag=!flag">挂载和卸载组件</button>
  </div>
</template>
<script>
import Header from './Header.vue'
import Life from './Life.vue'
import Axios from 'axios'
export default {
  data () {
    return {
      msg: 'home',
      flag: true,
      list: [],
      title: '父组件要传给子组件的值'
    }
  },
  components: {
    'v-header': Header,
    'v-life': Life
  },
  methods: {
    getHomeData () {
      // 请求数据
      var api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1'
      Axios.get(api).then((response) => {
        this.list = response.data.result
      }).catch((error) => {
        console.log(error)
      })
    },
    run (data) {
      // 子组件通过执行run方法传递过来的值
      console.log(data)
      alert('我是父组件的run方法')
    }
  }
}
</script>
<style lang="css" scope>
</style>
<template>
  <div>
    <h2>头部组件--{{msg}}--{{title}}</h2>
    <button @click="run(123)">子组件执行父组件的run方法</button>
    <!-- 通过传参,实现子组件给父组件传值 -->
    <button @click="getParent(123)">获取父组件的属性和方法</button>
  </div>
</template>
<script>
export default {
  data () {
    return {
      msg: '子组件自身属性'
    }
  },
  methods: {
    getParent () {
      alert(this.title)
      console.log(this.home.title)
    }
  },
  // 接受父组件传过来的值
  props: ['title', 'run', 'home']
}
</script>
<style>
</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞朋哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值