mirror of
				https://github.com/pure-admin/vue-pure-admin.git
				synced 2025-11-03 13:44:47 +08:00 
			
		
		
		
	feat: 函数式弹窗示例代码添加子组件prop为primitive类型的demo (#587)
				
					
				
			This commit is contained in:
		
							parent
							
								
									f613a79def
								
							
						
					
					
						commit
						3471e4a7e2
					
				@ -15,8 +15,10 @@ const props = withDefaults(defineProps<FormProps>(), {
 | 
				
			|||||||
  formInline: () => ({ user: "", region: "" })
 | 
					  formInline: () => ({ user: "", region: "" })
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// vue 规定所有的 prop 都遵循着单向绑定原则,不能在子组件中更改 prop 值,该 form.vue 文件为子组件
 | 
					// vue 规定所有的 prop 都遵循着单向绑定原则,直接修改 prop 时,Vue 会抛出警告。此处的写法仅仅是为了消除警告。
 | 
				
			||||||
// 如果需要拿到初始化的 prop 值并使得组件变量可修改,则需要在子组件定义一个新的变量接受这个子组件的 prop
 | 
					// 因为对一个 reactive 对象执行 ref,返回 Ref 对象的 value 值仍为传入的 reactive 对象,
 | 
				
			||||||
 | 
					// 即 newFormInline === props.formInline 为 true,所以此处代码的实际效果,仍是直接修改 props.formInline。
 | 
				
			||||||
 | 
					// 但该写法仅适用于 props.formInline 是一个对象类型的情况,原始类型需抛出事件
 | 
				
			||||||
// 推荐阅读:https://cn.vuejs.org/guide/components/props.html#one-way-data-flow
 | 
					// 推荐阅读:https://cn.vuejs.org/guide/components/props.html#one-way-data-flow
 | 
				
			||||||
const newFormInline = ref(props.formInline);
 | 
					const newFormInline = ref(props.formInline);
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										22
									
								
								src/views/components/dialog/formPrimitive.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/views/components/dialog/formPrimitive.vue
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					<script setup lang="ts">
 | 
				
			||||||
 | 
					import { useVModel } from "@vueuse/core";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 声明 props 类型
 | 
				
			||||||
 | 
					export interface FormProps {
 | 
				
			||||||
 | 
					  data: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 声明 props 默认值
 | 
				
			||||||
 | 
					// 推荐阅读:https://cn.vuejs.org/guide/typescript/composition-api.html#typing-component-props
 | 
				
			||||||
 | 
					const props = withDefaults(defineProps<FormProps>(), {
 | 
				
			||||||
 | 
					  data: () => ""
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 使用 vueuse 的双向绑定工具
 | 
				
			||||||
 | 
					const emit = defineEmits(["update:data"]);
 | 
				
			||||||
 | 
					const data = useVModel(props, "data", emit);
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<template>
 | 
				
			||||||
 | 
					  <el-input class="!w-[220px]" v-model="data" placeholder="请输入内容" />
 | 
				
			||||||
 | 
					</template>
 | 
				
			||||||
@ -3,6 +3,7 @@ import { useRouter } from "vue-router";
 | 
				
			|||||||
import { h, createVNode, ref } from "vue";
 | 
					import { h, createVNode, ref } from "vue";
 | 
				
			||||||
import { message } from "@/utils/message";
 | 
					import { message } from "@/utils/message";
 | 
				
			||||||
import forms, { type FormProps } from "./form.vue";
 | 
					import forms, { type FormProps } from "./form.vue";
 | 
				
			||||||
 | 
					import formPrimitive from "./formPrimitive.vue";
 | 
				
			||||||
import { cloneDeep, debounce } from "@pureadmin/utils";
 | 
					import { cloneDeep, debounce } from "@pureadmin/utils";
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
  addDialog,
 | 
					  addDialog,
 | 
				
			||||||
@ -316,7 +317,10 @@ function onFormTwoClick() {
 | 
				
			|||||||
  addDialog({
 | 
					  addDialog({
 | 
				
			||||||
    width: "30%",
 | 
					    width: "30%",
 | 
				
			||||||
    title: "结合Form表单(第二种方式)",
 | 
					    title: "结合Form表单(第二种方式)",
 | 
				
			||||||
    contentRenderer: () => h(forms, { formInline: formInline.value }),
 | 
					    contentRenderer: () =>
 | 
				
			||||||
 | 
					      h(forms, {
 | 
				
			||||||
 | 
					        formInline: formInline.value
 | 
				
			||||||
 | 
					      }),
 | 
				
			||||||
    closeCallBack: () => {
 | 
					    closeCallBack: () => {
 | 
				
			||||||
      message(
 | 
					      message(
 | 
				
			||||||
        `当前表单数据为 姓名:${formInline.value.user} 城市:${formInline.value.region}`
 | 
					        `当前表单数据为 姓名:${formInline.value.user} 城市:${formInline.value.region}`
 | 
				
			||||||
@ -338,7 +342,9 @@ function onFormThreeClick() {
 | 
				
			|||||||
    width: "30%",
 | 
					    width: "30%",
 | 
				
			||||||
    title: "结合Form表单(第三种方式)",
 | 
					    title: "结合Form表单(第三种方式)",
 | 
				
			||||||
    contentRenderer: () =>
 | 
					    contentRenderer: () =>
 | 
				
			||||||
      createVNode(forms, { formInline: formThreeInline.value }),
 | 
					      createVNode(forms, {
 | 
				
			||||||
 | 
					        formInline: formThreeInline.value
 | 
				
			||||||
 | 
					      }),
 | 
				
			||||||
    closeCallBack: () => {
 | 
					    closeCallBack: () => {
 | 
				
			||||||
      message(
 | 
					      message(
 | 
				
			||||||
        `当前表单数据为 姓名:${formThreeInline.value.user} 城市:${formThreeInline.value.region}`
 | 
					        `当前表单数据为 姓名:${formThreeInline.value.user} 城市:${formThreeInline.value.region}`
 | 
				
			||||||
@ -373,6 +379,26 @@ function onFormFourClick() {
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 子组件 prop 为 primitive 类型的 demo
 | 
				
			||||||
 | 
					const formPrimitiveParam = ref("Hello World");
 | 
				
			||||||
 | 
					const resetFormPrimitiveParam = ref(formPrimitiveParam.value);
 | 
				
			||||||
 | 
					function onFormPrimitiveFormClick() {
 | 
				
			||||||
 | 
					  addDialog({
 | 
				
			||||||
 | 
					    width: "30%",
 | 
				
			||||||
 | 
					    title: "子组件 prop 为 primitive 类型 demo",
 | 
				
			||||||
 | 
					    contentRenderer: () =>
 | 
				
			||||||
 | 
					      h(formPrimitive, {
 | 
				
			||||||
 | 
					        data: formPrimitiveParam.value,
 | 
				
			||||||
 | 
					        "onUpdate:data": val => (formPrimitiveParam.value = val)
 | 
				
			||||||
 | 
					      }),
 | 
				
			||||||
 | 
					    closeCallBack: () => {
 | 
				
			||||||
 | 
					      message(`当前表单内容:${formPrimitiveParam.value}`);
 | 
				
			||||||
 | 
					      // 重置表单数据
 | 
				
			||||||
 | 
					      formPrimitiveParam.value = resetFormPrimitiveParam.value;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function onBeforeCancelClick() {
 | 
					function onBeforeCancelClick() {
 | 
				
			||||||
  addDialog({
 | 
					  addDialog({
 | 
				
			||||||
    title: "点击底部取消按钮的回调",
 | 
					    title: "点击底部取消按钮的回调",
 | 
				
			||||||
@ -474,6 +500,9 @@ function onBeforeSureClick() {
 | 
				
			|||||||
      <el-button @click="onFormFourClick">
 | 
					      <el-button @click="onFormFourClick">
 | 
				
			||||||
        结合Form表单(第四种方式)
 | 
					        结合Form表单(第四种方式)
 | 
				
			||||||
      </el-button>
 | 
					      </el-button>
 | 
				
			||||||
 | 
					      <el-button @click="onFormPrimitiveFormClick">
 | 
				
			||||||
 | 
					        子组件 prop 为 primitive 类型
 | 
				
			||||||
 | 
					      </el-button>
 | 
				
			||||||
    </el-space>
 | 
					    </el-space>
 | 
				
			||||||
    <el-divider />
 | 
					    <el-divider />
 | 
				
			||||||
    <el-space wrap>
 | 
					    <el-space wrap>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user