vue自定义view,传值和事件回调

传值

  • 向子组件中传值,在子组件props中定义要传递的值
props: {
   
    text: ""
  },
  • 在父组件中调用
<TitleBarView text="你好"

事件回调

  • emit后,第一个参数为定义的事件名,第二个参数为向外传出的参数
  • 自定义一个事件
@click="$emit('clickEvent','Hello from child component!')"
  • 也可以展开写

@click="handleClick();"

methods:{
   
    handleClick() {
   
      this.$emit('clickEvent', 'Hello from child component!2')
    }
  }
  • 调用自定义点击事件,打印传出的参数
<TitleBarView text="你好" @clickEvent="titleClick"/>

  methods: {
   
    titleClick(param){
   
      //此处打印出,子组件中传出的参数
      console.log(param)
    },
  }

完整代码

  • 调用方式
<script>

import TitleBarView from "@/components/views/testing/chirld/TitleBarView.vue";

export default {
   

  components: {
   TitleBarView},

  methods: {
   
    titleClick(param){
   
      console.log(param)
    },
  }

}

</script>

<template>

  <TitleBarView text="你好" @clickEvent="titleClick"/>

</template>

<style scoped lang="scss">

</style>
  • 自定义控件TitleBarView
<template>
  <div>
    <div class="main-1">
      <el-image class="image" @click="$emit('clickEvent')" :src="pub.getAssetsFile('icon_16_jiantou_zuo.webp')"></el-image>
      <el-text class="title-1">{
   {
    text }}</el-text>
    </div>
  </div>
</template>

<script>
import usePub from "@/utils/pub-use"

export default {
   
  name: "TitleView",
  props: {
   
    text: ""
  },
  data() {
   
    return {
   
      pub: usePub,
    }
  }
}
</script>

<style scoped>

.main-1 {
   
  display: flex;
  flex-direction: row;
  /*margin-top: 16px;*/
  width: 100%;
  height: 56px;
  background: #FFFFFF;
  box-shadow: 0 3px 6px 0 rgba(6, 40, 120, 0.12);
  border-radius: 4px;

  .image {
   
    width: 20px;
    height: 20px;
    margin: auto 16px auto 16px;
  }

  .title-1 {
   
    /*background: red;*/
    width: auto;
    font-size: 20px;
    font-weight: 500;
    color: #062878;
    line-height: 28px;
  }
}


</style>

相关推荐

  1. vue定义view事件

    2024-02-19 06:44:02       48 阅读
  2. vue3利用定义事件v-model实现父子

    2024-02-19 06:44:02       61 阅读
  3. Vue定义事件

    2024-02-19 06:44:02       48 阅读
  4. Vue 定义事件

    2024-02-19 06:44:02       35 阅读
  5. vue 定义事件子组件方法调用

    2024-02-19 06:44:02       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 06:44:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 06:44:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 06:44:02       82 阅读
  4. Python语言-面向对象

    2024-02-19 06:44:02       91 阅读

热门阅读

  1. mongodb常用命令

    2024-02-19 06:44:02       46 阅读
  2. ACM/NOI/CSP比赛经验分享

    2024-02-19 06:44:02       47 阅读
  3. 阿里云香港服务器是cn2线路吗?

    2024-02-19 06:44:02       61 阅读
  4. 目标检测中AP50 AP75 APs APm APl 含义

    2024-02-19 06:44:02       49 阅读
  5. Leetcode 16-20题

    2024-02-19 06:44:02       57 阅读
  6. 【uniapp】自定义步骤条样式

    2024-02-19 06:44:02       44 阅读
  7. uni-app自定义tabbar(van-tabbar)

    2024-02-19 06:44:02       48 阅读