web前端之vue组件传参、各种传参的不同写法、语法糖


vue


vue3

语法糖(一)

子组件
html

<template>
  <div>
    <el-dialog
      v-model="isDialog"
      :title="titleObj[title]"
      width="50%"
      append-to-body
    >
      <el-form :model="dialogForm" label-width="68">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="姓名" required>
              <el-input
                class="w_100_"
                v-model="dialogForm.name"
                placeholder="请输入姓名"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄">
              <el-input
                class="w_100_"
                v-model="dialogForm.age"
                placeholder="请输入年龄"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row class="mt_20">
        <el-col class="d_f jc_fe" :span="24">
          <el-button @click="handleCancel">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script name="FormPanel" setup>
// 这个emit很重要
const emit = defineEmits(["handleFormCallback"]);
let info = reactive({
     
    isDialog: false,
    title: "add",
    titleObj: {
     
      add: "新增",
      edit: "编辑",
    },
    dialogForm: {
     
      // 名称
      name: "",
      // 年龄
      age: ""
    },
  }),
  {
      isDialog, title, titleObj, dialogForm } = toRefs(info);


/**
 * 确认(提交)
 */
async function handleSubmit() {
     
  console.log("表单数据: ", dialogForm.value);
  // 提交成功后触发父组件事件
  emit("handleFormCallback", title);
}
/**
 * 取消
 */
function handleCancel() {
     
  isDialog.value = !isDialog;
}
/**
 * 父组件执行
 * @param {String} id 行id
 * @param {String} key 标题类型
 */
async function handleOpenFormPanel(id = "", key = "add") {
     
  title = key;

  if (key === "add") {
     
    dialogForm.value = {
     
      // 名称
      name: "",
      // 年龄
      age: ""
    };
  } else {
     
    console.log("根据id获取详情: ", id);
  }

  nextTick(async () => {
     
    isDialog.value = true;
  });
}

// 暴露方法与属性(这个是重点)
// 如果不暴露,则父组件无法执行此函数
defineExpose({
     
  handleOpenFormPanel,
});
</script>


父组件

<template>
  <div>
  	<el-button type="success" @click="handleAdd('add')">新增</el-button>
  	<el-button type="primary" @click="handleEdit('id68', 'edit')">编辑</el-button>
    
    <!-- 新增/编辑面板 -->
    <form-panel ref="refFormPanel" @handleFormCallback="handleCallback"></form-panel>
  </div>
</template>

<script name="Parent" setup>
import FormPanel from "./components/formPanel.vue";
const refFormPanel = ref(null);

/**
 * 新增
 * @param {String} type 面板标题类型
 */
function handleAdd(type) {
     
  refFormPanel.value.handleOpenFormPanel("", type);
}
/**
 * 编辑
 * @param {String} id 行id
 * @param {String} type 面板标题类型
 */
function handleEdit(id, type) {
     
  refFormPanel.value.handleOpenFormPanel(id, type);
}
/**
 * 子组件回调
 */
function handleCallback(res = "") {
     
  console.log("子组件返回的数据: ", res);
}
</script>

语法糖(二)

相关推荐

  1. 后端各种格式混合vue前端

    2023-12-13 06:40:05       21 阅读
  2. Vue】组件

    2023-12-13 06:40:05       32 阅读
  3. Vue路由

    2023-12-13 06:40:05       26 阅读
  4. VUE父子组件问题

    2023-12-13 06:40:05       18 阅读
  5. vue跳转

    2023-12-13 06:40:05       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-13 06:40:05       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-13 06:40:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-13 06:40:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-13 06:40:05       18 阅读

热门阅读

  1. 【告警自动化处置脚本】

    2023-12-13 06:40:05       36 阅读
  2. 【环境系列】Linux虚拟机(Centos、Ubuntu)、云服务器

    2023-12-13 06:40:05       37 阅读
  3. webpack的使用

    2023-12-13 06:40:05       36 阅读
  4. 微信小程序---生命周期

    2023-12-13 06:40:05       41 阅读
  5. 微信小程序下载 base64 视频文件到本地相册

    2023-12-13 06:40:05       39 阅读
  6. kafka学习笔记--节点的服役与退役

    2023-12-13 06:40:05       45 阅读
  7. 小程序猎鹰策略组件

    2023-12-13 06:40:05       37 阅读
  8. 《C++新经典设计模式》之第2章 模板方法模式

    2023-12-13 06:40:05       33 阅读
  9. Spark SQL 的partitionBy() 动态分区

    2023-12-13 06:40:05       34 阅读