vue——动态组件

ゞ 浴缸里的玫瑰 2021-09-14 12:58 559阅读 0赞

通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,我们让多个组件可以使用同一个挂载点,并动态切换。根据 v-bind:is=”组件名” 中的组件名去自动匹配组件,如果匹配不到则不显示。

改变挂载的组件,只需要修改is指令的值即可。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 动态组件</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <button @click='toShow'>点击显示子组件</button>
  11. <component v-bind:is="which_to_show"></component>
  12. </div>
  13. <script>
  14. // 创建根实例
  15. new Vue({
  16. el: '#app',
  17. data:{
  18. which_to_show:'first'
  19. },
  20. methods:{
  21. toShow:function(){
  22. var arr = ["first","second","third"];
  23. var index = arr.indexOf(this.which_to_show);
  24. if(index<2){
  25. this.which_to_show = arr[index+1];
  26. }else{
  27. this.which_to_show = arr[0];
  28. }
  29. }
  30. },
  31. components:{
  32. first:{
  33. template:'<div>这是子组件1<div>'
  34. },
  35. second:{
  36. template:'<div>这是子组件2<div>'
  37. },
  38. third:{
  39. template:'<div>这是子组件3<div>'
  40. },
  41. }
  42. })
  43. </script>
  44. </body>
  45. </html>

20171227194144226

#keep-alive

动态切换掉的组件(非当前显示的组件)是被移除掉了,如果把切换出去的组件保留在内存中,可以保留它的状态或避免重新渲染。为此可以添加一个 keep-alive 指令参数:

  1. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 动态组件</title> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> </head> <body> <div id="app"> <button @click='toShow'>点击显示子组件</button> <!----或者<component v-bind:is="which_to_show" keep-alive></component>也行-----> <keep-alive> <component v-bind:is="which_to_show" ></component> </keep-alive> </div> <script> // 创建根实例 new Vue({ el: '#app', data:{ which_to_show:'first' }, methods:{ toShow:function(){ var arr = ["first","second","third"]; var index = arr.indexOf(this.which_to_show); if(index<2){ this.which_to_show = arr[index+1]; }else{ this.which_to_show = arr[0]; } console.log(this.$children); } }, components:{ first:{ template:'<div>这是子组件1<div>' }, second:{ template:'<div>这是子组件2<div>' }, third:{ template:'<div>这是子组件3<div>' }, } }) </script> </body> </html>

说明:

初始情况下,vm.$children属性中只有一个元素(first组件),

点击按钮切换后,vm.$children属性中有两个元素,

再次切换后,则有三个元素(三个子组件都保留在内存中)。

之后无论如何切换,将一直保持有三个元素。

actived钩子

可以延迟执行当前的组件。

具体用法来说,activate是和template、data等属性平级的一个属性,形式是一个函数,函数里默认有一个参数,而这个参数是一个函数,执行这个函数时,才会切换组件。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 动态组件</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <button @click='toShow'>点击显示子组件</button>
  11. <!----或者<component v-bind:is="which_to_show" keep-alive></component>也行----->
  12. <keep-alive>
  13. <component v-bind:is="which_to_show" ></component>
  14. </keep-alive>
  15. </div>
  16. <script>
  17. // 创建根实例
  18. var vm = new Vue({
  19. el: '#app',
  20. data: {
  21. which_to_show: "first"
  22. },
  23. methods: {
  24. toShow: function () { //切换组件显示
  25. var arr = ["first", "second", "third", ""];
  26. var index = arr.indexOf(this.which_to_show);
  27. if (index < 2) {
  28. this.which_to_show = arr[index + 1];
  29. } else {
  30. this.which_to_show = arr[0];
  31. }
  32. console.log(this.$children);
  33. }
  34. },
  35. components: {
  36. first: { //第一个子组件
  37. template: "<div>这里是子组件1</div>"
  38. },
  39. second: { //第二个子组件
  40. template: "<div>这里是子组件2,这里是延迟后的内容:{
  41. {hello}}</div>",
  42. data: function () {
  43. return {
  44. hello: ""
  45. }
  46. },
  47. activated: function (done) { //执行这个参数时,才会切换组件
  48. console.log('hhh')
  49. var self = this;
  50. var startTime = new Date().getTime(); // get the current time
  51. //两秒后执行
  52. while (new Date().getTime() < startTime + 2000){
  53. self.hello='我是延迟后的内容';
  54. }
  55. }
  56. },
  57. third: { //第三个子组件
  58. template: "<div>这里是子组件3</div>"
  59. }
  60. }
  61. });
  62. </script>
  63. </body>
  64. </html>

20171227210135643
当切换到第二个组件的时候,会先执行activated钩子,会在两秒后显示组件2.起到了延迟加载的作用。

发表评论

表情:
评论列表 (有 0 条评论,559人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue 动态组件

    动态组件 通过使用保留的 <component> 元素,并对其 is 特性进行动态绑定,你可以在同一个挂载点动态切换多个组件: var vm = new Vue

    相关 Vue动态组件

    Vue动态组件 1、序言 2、实例 1、序言   在页面应用程序中,经常会遇到多标签页面,在Vue.js中,可以通过动态组件来实现。组件的动态切换是通