• <small id='OGly1'></small><noframes id='OGly1'>

    <tfoot id='OGly1'></tfoot>

    1. <legend id='OGly1'><style id='OGly1'><dir id='OGly1'><q id='OGly1'></q></dir></style></legend>

      <i id='OGly1'><tr id='OGly1'><dt id='OGly1'><q id='OGly1'><span id='OGly1'><b id='OGly1'><form id='OGly1'><ins id='OGly1'></ins><ul id='OGly1'></ul><sub id='OGly1'></sub></form><legend id='OGly1'></legend><bdo id='OGly1'><pre id='OGly1'><center id='OGly1'></center></pre></bdo></b><th id='OGly1'></th></span></q></dt></tr></i><div id='OGly1'><tfoot id='OGly1'></tfoot><dl id='OGly1'><fieldset id='OGly1'></fieldset></dl></div>
          <bdo id='OGly1'></bdo><ul id='OGly1'></ul>
      1. vue js 看不懂keep alive

        vue js cant understand keep alive(vue js 看不懂keep alive)

        <tfoot id='HpFFj'></tfoot>
        • <bdo id='HpFFj'></bdo><ul id='HpFFj'></ul>

                <tbody id='HpFFj'></tbody>
              <i id='HpFFj'><tr id='HpFFj'><dt id='HpFFj'><q id='HpFFj'><span id='HpFFj'><b id='HpFFj'><form id='HpFFj'><ins id='HpFFj'></ins><ul id='HpFFj'></ul><sub id='HpFFj'></sub></form><legend id='HpFFj'></legend><bdo id='HpFFj'><pre id='HpFFj'><center id='HpFFj'></center></pre></bdo></b><th id='HpFFj'></th></span></q></dt></tr></i><div id='HpFFj'><tfoot id='HpFFj'></tfoot><dl id='HpFFj'><fieldset id='HpFFj'></fieldset></dl></div>

              <legend id='HpFFj'><style id='HpFFj'><dir id='HpFFj'><q id='HpFFj'></q></dir></style></legend>

              • <small id='HpFFj'></small><noframes id='HpFFj'>

                1. 本文介绍了vue js 看不懂keep alive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用以下代码进行测试:
                  https://jsfiddle.net/b2qj69o1/25/

                  I'm doing tests with this code:
                  https://jsfiddle.net/b2qj69o1/25/

                    <button
                      v-for="tab in tabs"
                      v-bind:key="tab.name"
                      v-bind:class="['tab-button', { active: currentTab.name === tab.name }]"
                      v-on:click="currentTab = tab"
                    >{{ tab.name }}</button>
                  
                    <component
                      v-bind:is="currentTab.component"
                      class="tab"
                    ></component>
                  

                  还有js部分

                  var tabs = [
                    {
                      name: 'Home', 
                      component: function() { 
                      alert();  // test
                      return new Promise((resolve, reject) => resolve({ 
                        template: '<div>Home component</div>' 
                      }))
                      }
                    },
                    {
                      name: 'Posts',
                      component: {
                        template: '<div>Posts component</div>'
                      }
                    },
                    {
                      name: 'Archive',
                      component: {
                        template: '<div>Archive component</div>',
                      }
                    }
                  ]
                  new Vue({
                    el: '#dynamic-component-demo',
                    data: {
                      tabs: tabs,
                      currentTab: tabs[1]
                    }
                  })
                  

                  我做了 alert() 测试看看 vue 是否会重新渲染组件.我看到无论是否使用 <keep-alive> 包装 <component>,如果仅在我第一次进入 Home 选项卡时调用 alert().所以我有两个问题:
                  1. keep-alive到底是做什么的?因为似乎无论如何该组件只创建一次.
                  2. vue 是否可以显示/隐藏选项卡,而不是替换单个 DOM 元素?但是在组件显示出来之前还是不要加载它.

                  I did the alert() test to see whether vue will re-render the component. I see that with or without wraping <component> with <keep-alive>, the alert() if called only the first time I enter the Home tab. So I have two questions:
                  1. What exactly keep-alive does? cause it seems that anyway the component is created only once.
                  2. Is it possible for vue to show/hide the tabs, instead of replacing single DOM element? But still not to load the component until it is shown.

                  推荐答案

                  您已将 alert() 调用放在异步返回组件定义的函数中.这个函数只会在第一次访问组件的定义时被调用.

                  You've put the alert() call in the function that asynchronously returns the component definition. This function will only get called the first time the definition of the component is accessed.

                  <keep-alive> 标签会缓存一个组件的实例,这样它就不会被破坏而需要重新挂载.

                  The <keep-alive> tag will cache an instance of a component so that it does not get destroyed and need to be mounted again.

                  使用您的示例,您可以看到每次切换到主页"选项卡时都会调用 mounted 挂钩:

                  Using your example, you can see that the mounted hook gets called any time you switch to the "Home" tab:

                  var tabs = [
                    {
                      name: 'Home', 
                      component: function() { 
                      return new Promise((resolve, reject) => resolve({ 
                        template: '<div>Home component</div>',
                        mounted() {
                          console.log('mounted')
                        }
                      }))
                      }
                    },
                    {
                      name: 'Posts',
                      component: {
                        template: '<div>Posts component</div>'
                      }
                    },
                    {
                      name: 'Archive',
                      component: {
                        template: '<div>Archive component</div>',
                      }
                    }
                  ]
                  
                  new Vue({
                    el: '#dynamic-component-demo',
                    data: {
                    	tabs: tabs,
                      currentTab: tabs[1]
                    }
                  })

                  .tab-button {
                    padding: 6px 10px;
                    border-top-left-radius: 3px;
                    border-top-right-radius: 3px;
                    border: 1px solid #ccc;
                    cursor: pointer;
                    background: #f0f0f0;
                    margin-bottom: -1px;
                    margin-right: -1px;
                  }
                  .tab-button:hover {
                    background: #e0e0e0;
                  }
                  .tab-button.active {
                    background: #e0e0e0;
                  }
                  .tab {
                    border: 1px solid #ccc;
                    padding: 10px;
                  }

                  <script src="https://unpkg.com/vue"></script>
                  
                  <div id="dynamic-component-demo" class="demo">
                    <button
                      v-for="tab in tabs"
                      v-bind:key="tab.name"
                      v-bind:class="['tab-button', { active: currentTab.name === tab.name }]"
                      v-on:click="currentTab = tab"
                    >{{ tab.name }}</button>
                  
                    <component
                      v-bind:is="currentTab.component"
                      class="tab"    
                    ></component>
                  </div>

                  但是,通过将动态组件包装在 <keep-alive> 标记中,您是在告诉 Vue 缓存对主路由组件的引用.所以 mounted 钩子只会在组件第一次实例化时被调用:

                  But, by wrapping the dynamic component in a <keep-alive> tag, you are telling Vue to cache a reference to the home route's component. So the mounted hook only gets called the first time the component is instantiated:

                  var tabs = [
                    {
                      name: 'Home', 
                      component: function() { 
                      return new Promise((resolve, reject) => resolve({ 
                        template: '<div>Home component</div>',
                        mounted() {
                          console.log('mounted')
                        }
                      }))
                      }
                    },
                    {
                      name: 'Posts',
                      component: {
                        template: '<div>Posts component</div>'
                      }
                    },
                    {
                      name: 'Archive',
                      component: {
                        template: '<div>Archive component</div>',
                      }
                    }
                  ]
                  
                  new Vue({
                    el: '#dynamic-component-demo',
                    data: {
                    	tabs: tabs,
                      currentTab: tabs[1]
                    }
                  })

                  .tab-button {
                    padding: 6px 10px;
                    border-top-left-radius: 3px;
                    border-top-right-radius: 3px;
                    border: 1px solid #ccc;
                    cursor: pointer;
                    background: #f0f0f0;
                    margin-bottom: -1px;
                    margin-right: -1px;
                  }
                  .tab-button:hover {
                    background: #e0e0e0;
                  }
                  .tab-button.active {
                    background: #e0e0e0;
                  }
                  .tab {
                    border: 1px solid #ccc;
                    padding: 10px;
                  }

                  <script src="https://unpkg.com/vue"></script>
                  
                  <div id="dynamic-component-demo" class="demo">
                    <button
                      v-for="tab in tabs"
                      v-bind:key="tab.name"
                      v-bind:class="['tab-button', { active: currentTab.name === tab.name }]"
                      v-on:click="currentTab = tab"
                    >{{ tab.name }}</button>
                  
                    <keep-alive>
                      <component
                        v-bind:is="currentTab.component"
                        class="tab"    
                      ></component>
                    </keep-alive>
                  </div>

                  这里是使用文档<keep-alive> 带有动态组件的标签.

                  Here's the documentation on using the <keep-alive> tag with dynamic components.

                  至于你的第二个问题:如果你想最初将所有选项卡添加到 DOM 并简单地隐藏非活动选项卡组件,然后使用 v-for 指令渲染每个选项卡,并使用v-show 指令仅显示活动选项卡.

                  As for your second question: if you want to add all of the tabs to the DOM initially and simply hide the inactive tab components, then render each tab using the v-for directive, and use the v-show directive to only show the active tab.

                  这是一个例子:

                  var tabs = [
                    {
                      name: 'Home', 
                      component: function() { 
                      return new Promise((resolve, reject) => resolve({ 
                        template: '<div>Home component</div>',
                      }))
                      }
                    },
                    {
                      name: 'Posts',
                      component: {
                        template: '<div>Posts component</div>'
                      }
                    },
                    {
                      name: 'Archive',
                      component: {
                        template: '<div>Archive component</div>',
                      }
                    }
                  ]
                  
                  new Vue({
                    el: '#dynamic-component-demo',
                    data: {
                    	tabs: tabs,
                      currentTab: tabs[1]
                    }
                  })

                  .tab-button {
                    padding: 6px 10px;
                    border-top-left-radius: 3px;
                    border-top-right-radius: 3px;
                    border: 1px solid #ccc;
                    cursor: pointer;
                    background: #f0f0f0;
                    margin-bottom: -1px;
                    margin-right: -1px;
                  }
                  .tab-button:hover {
                    background: #e0e0e0;
                  }
                  .tab-button.active {
                    background: #e0e0e0;
                  }
                  .tab {
                    border: 1px solid #ccc;
                    padding: 10px;
                  }

                  <script src="https://unpkg.com/vue"></script>
                  
                  <div id="dynamic-component-demo" class="demo">
                    <button
                      v-for="tab in tabs"
                      v-bind:key="tab.name"
                      v-bind:class="['tab-button', { active: currentTab.name === tab.name }]"
                      v-on:click="currentTab = tab"
                    >{{ tab.name }}</button>
                  
                    <component
                      v-for="tab in tabs"
                      v-bind:key="'component-' + tab.name"
                      v-bind:is="tab.component"
                      class="tab"
                      v-show="currentTab === tab"
                    ></component>
                  </div>

                  而且,如果我正确理解了您的最后一句话,如果您真的想在选项卡最初处于活动状态之前不创建选项卡组件,但是想要在另一个选项卡处于活动状态时隐藏选项卡的 HTML 内容,您需要跟踪数据属性中哪些选项卡已被激活,然后使用 v-if 指令最初阻止组件初始化.

                  And, if I understand your last sentence correctly, if you really want to not create a tab component until the tab is initially active, but then want to hide the tab's HTML content when another tab is active, you'd need to keep track of which tabs have been activated in a data property and then use the v-if directive to initially prevent the component from initializing.

                  类似这样的:

                  var tabs = [
                    {
                      name: 'Home', 
                      component: function() { 
                      return new Promise((resolve, reject) => resolve({ 
                        template: '<div>Home component</div>',
                      }))
                      }
                    },
                    {
                      name: 'Posts',
                      component: {
                        template: '<div>Posts component</div>'
                      }
                    },
                    {
                      name: 'Archive',
                      component: {
                        template: '<div>Archive component</div>',
                      }
                    }
                  ]
                  
                  new Vue({
                    el: '#dynamic-component-demo',
                    data: {
                    	tabs: tabs,
                      currentTab: tabs[1],
                      activatedTabs: {}
                    },
                    watch: {
                      currentTab: {
                        immediate: true,
                        handler(tab) {
                          this.$set(this.activatedTabs, tab.name, true);
                        }
                      }
                    }
                  })

                  .tab-button {
                    padding: 6px 10px;
                    border-top-left-radius: 3px;
                    border-top-right-radius: 3px;
                    border: 1px solid #ccc;
                    cursor: pointer;
                    background: #f0f0f0;
                    margin-bottom: -1px;
                    margin-right: -1px;
                  }
                  .tab-button:hover {
                    background: #e0e0e0;
                  }
                  .tab-button.active {
                    background: #e0e0e0;
                  }
                  .tab {
                    border: 1px solid #ccc;
                    padding: 10px;
                  }

                  <script src="https://unpkg.com/vue"></script>
                  
                  <div id="dynamic-component-demo" class="demo">
                    <button
                      v-for="tab in tabs"
                      v-bind:key="tab.name"
                      v-bind:class="['tab-button', { active: currentTab.name === tab.name }]"
                      v-on:click="currentTab = tab"
                    >{{ tab.name }}</button>
                  
                    <component
                      v-for="tab in tabs"
                      v-if="activatedTabs[tab.name]"
                      v-bind:key="'component-' + tab.name"
                      v-bind:is="tab.component"
                      class="tab"
                      v-show="currentTab === tab"
                    ></component>
                  </div>

                  这篇关于vue js 看不懂keep alive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                    1. <tfoot id='Qq7xJ'></tfoot>
                      <legend id='Qq7xJ'><style id='Qq7xJ'><dir id='Qq7xJ'><q id='Qq7xJ'></q></dir></style></legend>
                        <bdo id='Qq7xJ'></bdo><ul id='Qq7xJ'></ul>
                          <tbody id='Qq7xJ'></tbody>

                        1. <i id='Qq7xJ'><tr id='Qq7xJ'><dt id='Qq7xJ'><q id='Qq7xJ'><span id='Qq7xJ'><b id='Qq7xJ'><form id='Qq7xJ'><ins id='Qq7xJ'></ins><ul id='Qq7xJ'></ul><sub id='Qq7xJ'></sub></form><legend id='Qq7xJ'></legend><bdo id='Qq7xJ'><pre id='Qq7xJ'><center id='Qq7xJ'></center></pre></bdo></b><th id='Qq7xJ'></th></span></q></dt></tr></i><div id='Qq7xJ'><tfoot id='Qq7xJ'></tfoot><dl id='Qq7xJ'><fieldset id='Qq7xJ'></fieldset></dl></div>

                          <small id='Qq7xJ'></small><noframes id='Qq7xJ'>