子组件传递参数以及接收参数
<template>
<p>{{ props.msg2 }}</p>
<button @click="showToast" class="btn">打开 toast</button>
<!-- to 属性就是目标位置 -->
<teleport to="#teleport-target">
<div v-if="visible" class="toast-wrap">
<div class="toast-msg"></div>
</div>
</teleport>
</template>
<script setup lang="ts">
import { getCurrentInstance, inject, watch, ref } from 'vue';
const instance = getCurrentInstance()
const _this = instance.appContext.config.globalProperties
const name = inject('name')
const props = defineProps({
msg1: String,
msg2: {
type:String,
default:""
}
})
console.log(props);
watch(name, (newValue, oldValue) => {
console.log(name.value)
})
const visible = ref(false);
let timer;
const showToast = () => {
visible.value = true;
}
</script>
<style>
</style>
父组件传递参数
<template>
<div class="box">
<echart :option="picOptions" :id="'qweq-111'" :height="800"/>
<emit :msg2="name" @myClick="myClick"></emit>
</div>
</template>
<script setup lang="ts">
import { defineAsyncComponent, ref, provide } from 'vue';
import { picOption } from './config.js'
const picOptions = ref(picOption)
const emit = defineAsyncComponent(() => import('../../components/edit/index.vue'))
const name = ref('1111')
provide('name', name)
const myClick = (value: boolean) => {
console.log(value);
}
</script>
<style scoped>
.box {
width: 100%;
height: 800px;
}
</style>
使用类似react中的hooks,按模块划分
<template>
<el-table :data="tableData" style="width: 100%" class="list-table">
<el-table-column label="标题" width="180">
<template #default="scope">
<el-icon><timer /></el-icon>
<span style="margin-left: 10px">{{ scope.row.date }}</span>
</template>
</el-table-column>
<el-table-column label="价格" width="180">
<template #default="scope">
<el-icon><timer /></el-icon>
<span style="margin-left: 10px">{{ scope.row.name }}</span>
</template>
</el-table-column>
<el-table-column label="操作">
<template #default="scope">
<el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
>Edit</el-button
>
<el-button
size="mini"
type="danger"
@click="handleDelete(scope.$index, scope.row)"
>Delete</el-button
>
</template>
</el-table-column>
</el-table>
</template>
<script setup lang="ts">
import { ref, watch } from "vue";
import usetableHooks from './List/table'
const { tableData, handleEdit, handleDelete } = usetableHooks()
</script>
<style>
.el-main {
line-height: 40px;
}
</style>
import { reactive } from "vue";
export default function usetableHooks() {
const tableData = reactive([
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
])
const handleEdit = (index: number, row: any) => {
console.log(index, row)
}
const handleDelete = (index: number, row: any) => {
console.log(index, row)
}
return {
tableData,
handleEdit,
handleDelete,
};
}