跳到主要内容

语法

2023年04月24日
柏拉文
越努力,越幸运

一、基础插槽


<template>
<div>
<AComp>哈哈哈</AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
};
</script>

二、具名插槽


2.1 完整语法

<template>
<div>
<AComp>
<template v-slot:body1>哈哈哈</template>
<template v-slot:body2>嘻嘻嘻</template>
</AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
};
</script>

2.2 简写语法

<template>
<div>
<AComp>
<template #body1>哈哈哈</template>
<template #body2>嘻嘻嘻</template>
</AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
};
</script>

三、动态插槽


3.1 完整语法

<template>
<div>
<AComp>
<template v-slot:[slotName1]>哈哈哈</template>
<template v-slot:[slotName2]>嘻嘻嘻</template>
</AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data(){
return {
slotName1: "body1",
slotName2: "body2"
}
}
};
</script>

3.2 简写语法

<template>
<div>
<AComp>
<template #[slotName1]>哈哈哈</template>
<template #[slotName2]>嘻嘻嘻</template>
</AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data(){
return {
slotName1: "body1",
slotName2: "body2"
}
}
};
</script>

四、作用域插槽


4.1 完整语法

<template>
<div>
<AComp>
<template v-slot:body1="slotProps1"
>{{ slotProps1.a }} - {{ slotProps1.b }}</template
>
<template v-slot:body2="slotProps2"
>{{ slotProps2.b }} + {{ slotProps2.c }}</template
>
</AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
};
</script>

4.2 简写语法

<template>
<div>
<AComp>
<template #body1="slotProps1"
>{{ slotProps1.a }} - {{ slotProps1.b }}</template
>
<template #body2="slotProps2"
>{{ slotProps2.b }} + {{ slotProps2.c }}</template
>
</AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
};
</script>