vue 方法间传递数据对象参数

功能:将data对象作为参数传递给方法,在方法中对不同的参数进行相应操作。

如下,实现简单实现根据传入的不同p标签key对象,在相应p标签中显示相应内容:

<template>
  <div>
    <!-- 可以通过如下两种方式绑定标签内容 -->
    <p>{{map.p1Content}}</p>
    <p>{{map.p2Content}}</p>
    <p>{{map.p3Content}}</p>
    <p v-html="map.p4Content"></p>
  </div>
</template>

<script>
export default {
  name: 'TransferParam',
  data () {
    return {
      map: {
        p1Content: '',
        p2Content: '',
        p3Content: '',
        p4Content: '',
      },
    }
  },
  methods: {
    //   给对象赋值
    setPValue (contentKey, content) {
      this.map[contentKey] = content
    },
  },
  created () {
    //   调用方法
    this.setPValue('p1Content', "第一个p标签内容。")
    this.setPValue('p2Content', "第二个p标签内容。")
    this.setPValue('p3Content', "第三个p标签内容。")
    this.setPValue('p4Content', "第四个p标签内容。")
  }
}
</script>

效果图:
在这里插入图片描述
代码:https://gitee.com/crazywsp/JavaImprove/blob/master/VueRepo/yarndemo03/src/components/TransferParam.vue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中,你可以通过 props 属性将数据对象传递给组件。以下是一个示例代码,展示了如何传递数据对象作为参数给组件: ```vue <template> <div> <child-component :myObject="parentObject"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentObject: { name: 'John', age: 25, email: 'john@example.com' } }; } }; </script> ``` 在上述示例中,我们有一个父组件和一个子组件。父组件通过将数据对象 `parentObject` 作为 props 属性传递给子组件来传递数据对象参数。 子组件 `ChildComponent` 的定义如下: ```vue <template> <div> <p>Name: {{ myObject.name }}</p> <p>Age: {{ myObject.age }}</p> <p>Email: {{ myObject.email }}</p> </div> </template> <script> export default { props: { myObject: { type: Object, required: true } } }; </script> ``` 在子组件中,我们定义了一个名为 `myObject` 的 prop 属性,类型为 Object,并且设置为必需的。然后,我们可以在模板中通过 `{{ myObject.name }}`、`{{ myObject.age }}` 等方式来访问和展示数据对象的属性。 父组件通过使用 `:myObject="parentObject"` 来将 `parentObject` 数据对象传递给子组件的 `myObject` prop 属性。 这样,就可以在 Vue 组件中成功传递和使用数据对象作为参数了。你可以根据实际需求传递不同的数据对象,并在子组件中进行相应的操作和展示。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值