vue传参父到子,子到父,跳转等

页面传参:

父到子:props传参,写在子组件上:title="title"

// 父级页面
<template>
  <div>
    <DefaultPage :title="title"></DefaultPage>
  </div>
</template>
<script>

import DefaultPage from './DefaultPage'

export default {
  name: 'Demo',
  components: { DefaultPage },
  data () {
    return {
      title: "加载失败,请稍后重试~",
    };
  },
};
</script>
<style lang="scss" type="text/scss">
</style>

子页面:

<template>
  <div>
    <p class="title">{{ title }}</p>
    <slot></slot>
  </div>
</template>
<script>

export default {
  props: {
    title: {
      type: String,
      default: '',
    },
  },
  data () {
    return {}
  },
}
</script>

<style scoped>
.title {
  font-size: 15px;
}
</style>

=========================================================================

子到父:this.$emit

<!--父级页面 -->
<template>
  <div>
    <Card-price @getWallet="getWallet"></Card-price>
  </div>
</template>
<script>

import CardPrice from './CardPrice'

export default {
  name: 'Demo',
  components: { CardPrice },
  data () {
    return {
      isGetWallet: false,
    };
  },
  methods: {
    getWallet (params) {
      console.log(params, '子组件传的参数')
      this.isGetWallet = params.isGetWallet
    },
  }
};
</script>
<style lang="scss" type="text/scss">
</style>

子页面:

<!--子级页面 -->
<template>
  <div>子组件</div>
</template>

<script>

export default {
  name: "Card",
  data () {
    return {
    }
  },
  mounted () {
    this.$emit('getWallet', { isGetWallet: true })
  },
}
</script>
<style lang="scss" scoped>
</style>

=========================================================================

跳转传参和取参

跳转方式:
1、push (会添加新的页面)
传参query
this.$router.push({ name: "优惠券详情", query: { ticketId: 1 } })

取参:ticketId: this.$route.query.ticketId ? this.$route.query.ticketId : 0,

2、replace (会覆盖页面)

this.$router.replace({ path: '/demo?id=1', query: {isShow: true } })

取参:取参:isShow: this.$route.query.isShow ? this.$route.query.isShow : false,

取链接上带的参数 :https://www.baidu.com/s?ie=UTF-8
 

取链接上的参数ie的值 :https://www.baidu.com/s?ie=UTF-8

function getQueryString (name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}
getQueryString('ie')

// "UTF-8"

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值