vue父组件向子组件传参

78 篇文章 107 订阅
需求:头部为一个公用组件,跳转页面标题切换,同时控制返回键显示隐藏父组件代码(引用子组件) 同时子组件上传递参数textTitle和ifReturn:<template> <div> <headAssembly textTitle="消息" ifReturn="0"/> <div><template>...
摘要由CSDN通过智能技术生成

需求:头部为一个公用组件,跳转页面标题切换,同时控制返回键显示隐藏

 

父组件代码(引用子组件) 同时子组件上传递参数textTitleifReturn

<template>
  <div>
    <headAssembly textTitle="消息" ifReturn="0"/>
  <div>
<template>
<script>
import TabSwitching from "../components/tabs/TabSwitching.vue"
export default {
  name: 'index_news',
  data () {
    return {

    }
  },
  components: {
    headAssembly
  },
}
</script>

  

子组件代码(接收传参显示)通过props属性接收参数textTitle直接渲染页面上,ifReturn判断显示,同时加返回上一页方法

<template>
  <div>
    <div class="headTop">
      {{textTitle}}
      <div class="head_left" v-if="ifReturn==1" @click="topRevious"> 
        <img class="Return_img" src="../../../static/img/Return1.png"/>
      </div>
    </div>
    <div class="box"></div>
  </div>
</template>

<script>
export default {
  name: "feedShopping",
  props:['textTitle','ifReturn'],
  data() {
    return { 
    };
  },
  mounted() {
    
  },
  methods: {
  
    topRevious() {
      this.$router.go(-1);
    }
  }
};
</script>

补充:

         怎么在 methods 中获取子组件 props 中的参数

<template>
  <div>
    <div class="headTop">
      {{textTitle}}
      <div class="head_left" v-if="ifReturn==1" @click="topRevious"> 
        <img class="Return_img" src="../../../static/img/Return1.png"/>
      </div>
    </div>
    <div class="box"></div>
  </div>
</template>

<script>
export default {
  name: "feedShopping",
  props:['textTitle','ifReturn'],
  data() {
    return { 
        text:'',
    };
  },
    //监听
  watch: {
    textTitle: function(newVal,oldVal){
      this.ObtainId(newVal)//newVal就是监听的textTitle
    }
  },
  mounted() {
    
  },
  methods: {
    //监听获取
    ObtainId(newVal){
       this.text=newVal
    },
    topRevious() {
      this.$router.go(-1);
    }
  }
};
</script>

监听textTitle的值,当它由空转变时就会触发,这时候就能取到了,拿到值后要做的处理方法也需要在watch里面执行

 

更多技巧请查看vue专栏   https://blog.csdn.net/qq_42221334/column/info/27230/1

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值