vue动态组件与插件到底是什么?


根组件

<template>

  <div id="app">

    <button @click="goto('InBox')" :class="{ current: currentComponent === 'InBox' }">收邮件</button>

    <button @click="goto('PostMail')" :class="{ current: currentComponent === 'PostMail' }">发邮件</button>

    <button @click="goto('RecycleBin')" :class="{ current: currentComponent === 'RecycleBin' }">垃圾箱</button>

    <hr />



    <InBox v-if="currentComponent==='InBox'" ></InBox>

    <PostMail v-if="currentComponent==='PostMail'" ></PostMail>

    <RecycleBin v-if="currentComponent==='RecycleBin'" ></RecycleBin>

  </div>

</template>



<script>

import InBox from "./components/InBox.vue";

import PostMail from "./components/PostMail.vue";

import RecycleBin from "./components/RecycleBin.vue";



export default {

  name: "App",

  data() {

    return {

      currentComponent: "InBox"

    };

  },

  components: {

    InBox,

    PostMail,

    RecycleBin

  },

  methods: {

    goto(target) {

      this.currentComponent = target;

    },

    created() {

      console.log(this.currentComponent + ":created");

    },

    destroyed() {

      console.log(this.currentComponent + "InBox:destroyed");

    }

  },

};

</script>



<style>

.current {

  background: yellow;

}

</style>



子组件1===============================================

<template>

  <div>

    <ul>

      <li v-for="item of items" :key="item">

        <input type="checkbox" />

        {{ item }}

      </li>

    </ul>

  </div>

</template>



<script>

export default {

  name: "InBox",

  data() {

    return {

      items: ["111111", "22222222222", "aaaaaaaa", "3333333"]

    };

  },



  created() {

    console.log("InBox:created");

  },

  destroyed() {

    console.log("InBox:destroyed");

  }

};

</script>



子组件2===============================================

<template>

  <div>PostMail</div>

</template>



<script>

export default {

  name: "PostMail",



  created() {

      console.log("PostMail:created");

    },

    destroyed() {

      console.log("PostMail:destroyed");

    }

  

};

</script>



子组件3===============================================

<template>

  <div>RecycleBin</div>

</template>



<script>

export default {

  name: "RecycleBin",

  created() {

    console.log("RecycleBin:created");

  },

  destroyed() {

    console.log("RecycleBin:destroyed");

  }

};

</script>



在这里插入图片描述

我们会发现,当组件切换的时候,都会触发组件的销毁和重建。首先,性能不好。其次,会丢失组件状态

我们可以通过keep-alive 组件 来解决上述问题


<keep-alive>

	<component :is="currentComponent"></component>

</keep-alive>



在这里插入图片描述

现在就处理了上述问题,同时 keep-alive 会触发 activateddeactivated 两个生命周期函数

  1. keep-alive 组件激活时调用

  2. keep-alive 组件停用时调用

插件

============================================================

插件通常是用来给 vue 提供扩展功能的一种方式

插件通常只有两个来源:

  1. 自定义

  2. 第三方

插件需要一个引入和使用就可以了注意!插件的引入和使用都需要写在main.js内,且引入需要写在使用之前,使用需要写在new vue之前

  1. 引入

import ppt from './AK'



  1. 使用

Vue.use(ppt);

#### 后话

-------------------------------------------------------------



对于面试,说几句个人观点。



面试,说到底是一种考试。正如我们一直批判应试教育脱离教育的本质,为了面试学习技术也脱离了技术的初心。但考试对于人才选拔的有效性是毋庸置疑的,几千年来一直如此。除非你有实力向公司证明你足够优秀,否则,还是得乖乖准备面试。这也并不妨碍你在通过面试之后按自己的方式学习。
其实在面试准备阶段,个人的收获是很大的,我也认为这是一种不错的学习方式。首先,面试问题大部分基础而且深入,这些是平时工作的基础。就好像我们之前一直不明白学习语文的意义,但它的意义就在每天的谈话间。

所谓面试造火箭,工作拧螺丝。面试往往有更高的要求,也迫使我们更专心更深入地去学习一些知识,也何尝不是一种好事。
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值