vue组件:keep-alive组件和插槽

本文介绍了Vue中动态组件的使用,通过`<component :is>`实现tab栏切换,并详细阐述了`<keep-alive>`组件如何进行缓存管理,包括`include`属性的配置以及`activated`和`deactivated`生命周期钩子的运用。同时,文章还探讨了Vue的插槽机制,包括基本插槽和作用域插槽的使用,展示了如何在父子组件间传递内容和结构。
摘要由CSDN通过智能技术生成

第一部分:keep-alive组件

一.动态组件(component标签)

语法:.<component :is="变量"></component>

注意::is="组件注册后的标签名字符串或data变量"

  • 不能直接拿注册标签名赋值使用
需求:tab栏切换:
component标签的原理:在父组件中对子组件创建销毁
<template>
 <div>
  我是父组件App.vue
  <!--子组件名 <Myone /><Mytow /><Mythree /> -->
  <component :is="comName"></component>
  //点击button按钮改变变量comName的值完成tab栏切换
  <button @click="comName = 'Myone'">点我切换页面Mytow</button>
  <button @click="comName = 'Mytow'">点我切换页面Mytow</button>
  <button @click="comName = 'Mythree'">点我切换页面Mythree</button>
 </div>
</template>
<script>
import Myone from "./compoents/Myone.vue";
import Mytow from "./compoents/Mytow.vue";
import Mythree from "./compoents/Mythree.vue";
export default {
  // 声明变量
  data() {
    return { comName: "Myone" };
  },
  components: {
    Myone,
    Mytow,
    Mythree,
  },
};
</script>

created创建组件生命周期钩子和destroyed销毁组件生命周期钩子

注意:只有使用了component标签的子组件才能使用这两货

<script>
export default {
  // 生命周期created创建组件
  // 生命周期destroyed销毁组件
  // 只有使用了component标签的子组件才能使用这两货
  created() {
    console.log("我是Mythree生命周期created创建组件");
  },
  destroyed() {
    console.log("我是Mythree生命周期destroyed销毁组件");
  },
};
</script>

二.keep-alive在component标签中的应用

 <keep-alive ></keep-alive>标签解决缓存

语法 :

     1. include="组件名1,组件名2..."

     2. :include="['组件名1', '组件名2']"

<template>
 <div>
  我是父组件App.vue
  <!-- 
   <keep-alive ></keep-alive>标签解决缓存
  语法 :1. include="组件名1,组件名2..."
       2. :include="['组件名1', '组件名2']"
  -->
 <!-- 
     在动态的include标签中写了名字的子组件,在tab栏切换中不会销毁,
     而是会出现失去活力状态deactivated (隐藏)和激活状态activated (显示) 
  -->
   <keep-alive :include="['Myone', 'Mytow', 'Mythree']">
   <component :is="comName"></component>
   </keep-alive>
   component :is="comName"></component>
   <button @click="comName = 'Myone'">点我切换页面Mytow</button>
   <button @click="comName = 'Mytow'">点我切换页面Mytow</button>
   <button @click="comName = 'Mythree'">点我切换页面Mythree</button>
  </div>
</template>
<script>
import Myone from "./compoents/Myone.vue";
import Mytow from "./compoents/Mytow.vue";
import Mythree from "./compoents/Mythree.vue";
export default {
  // 声明变量
  data() {
    return { comName: "Myone" };
  },
  components: {
    //组件注册
    Myone,
    Mytow,
    Mythree,
  },
};
</script>

activated激活生命周期函数和deactivated失去活力生命周期钩子

<script>
export default {
  // 生命周期created创建组件
  // 生命周期destroyed销毁组件
  // 只有使用了component标签的子组件才能使用这两货
  created() {
    console.log("我是Mythree生命周期created创建组件");
  },
  destroyed() {
    console.log("我是Mythree生命周期destroyed销毁组件");
  },
  // deactivated失去活力
  // deactivated激活
  // 只有使用了<keep-alive>标签的子组件才能使用这两货
  deactivated() {
    console.log("我是Mythree生命周期deactivated失去活力");
  },
  activated() {
    console.log("我是Mythree生命周期deactivated激活");
  },
};
</script>

第二部分:插槽

一.插槽基本使用

父组件中(插入)

父组件中
<template>
 <div>
  <p>我是父组件App.vue</p>
  <Mythree>
 <!--
   语法如下:
   template必须要起一个名字和子组件标签<slot name="one对应"> 这里的one随意起 
 -->
   <template v-slot:one> <h2>你好我是父组件插入进来的内容</h2></template>
 <!-- v-slot可以简写#one -->
  </Mythree>
 </div>
</template>
//以下部分可以不看
<script>
 import Mythree from "./compoents/Mythree.vue";
 export default {
  components: {
    Mythree,
  },
 };
 </script>

子组件(设置插入点)

子组件中
<template >
 <div>
   <p>我是Mythree</p>
   <!-- 当一个组件内有2处以上需要外部传入标签的地方 -->
   <!-- 
    子组件语法: <slot name="one">默认展示内容</slot>
    -->
   <slot name="one"></slot>
 </div>
</template>

二.作用域插槽(子传父:{数据}+结构)

 注意:子传父可以利用v-bind:a="变量",传输变量到父组件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个好好的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值