zoukankan      html  css  js  c++  java
  • vue 点击当前路由重新加载该路由

    需求:点击当前路由实现数据请求页面刷新 -- router.push(当前路由)并不会执行

    刷新页面

    1、window.reload()

    2、this.$router.go(0)
     
    但是这两种刷新时,整个浏览器进行了重新加载,跳跃,不平滑,体验不好
     
    3、v-if ,控制router-view的显示或隐藏,从而控制页面的再次加载,
     
    如果是菜单,感觉控制菜单显隐不太好
     
    4、借助第三方组件实现两次路由跳转
    思路:   
      1)判断是否点击的当前路由,如果是,则跳转到空白的第三方组件,并传递当前路由。
      2)在空白组件中 created 的生命周期中接受参数,并执行页面跳转,此时页面不会显示任何内容就开始进行跳转,所以速度的问题不用担心,视觉上的效果就是点击当前路由后,页面刷新请求数据。实际路由已经跳转了两次。
    //菜单点击的回调
    handleSelect(key, keyPath) {
          if(key === this.$route.path){
            // this.$router.go(0)
            // this.$router.push(key)
            this.$router.push({
                  path: 'black',
                  query: key
            })
          }
    }
    // 空白模板 
    <template>
      <div></div>
    </template>
    <script>
      export default {
        data() {
          return {};
        },
        created() {
          const path = this.$route.query;
          this.$router.push({
            path
          });
        }
      };
    </script>
    <style>
    </style>
          console.log('设置或获取对象指定的文件名或路径               -- window.location.pathname---',   window.location.pathname);
          console.log('设置或获取整个 URL 为字符串                   -- window.location.href--------',  window.location.href);
          console.log('设置或获取location或URL的hostname和port号码   -- window.location.host-------',   window.location.host);
          console.log('设置或获取与 URL 关联的端口号码               -- window.location.port--------',   window.location.port);
          console.log('设置或获取 URL 的协议部分                     -- window.location.protocol---',    window.location.protocol);
          console.log('设置或获取 href 属性中在井号“#”后面的分段      -- window.location.hash-------',    window.location.hash);
          console.log('设置或获取 href 属性中跟在问号后面的部分       -- window.location.search------',   window.location.search);
          console.log('获取当前网页的域名                            -- document.domain-------------',   document.domain);
          console.log('获取当前网页的路由                            --this.$route.path-------------',   this.$route.path);

  • 相关阅读:
    GZS与小公园(DFS)
    II play with GG(思维规律)
    bearBaby loves sleeping(BFS)
    湖南大学新生赛C,G,J题解
    bootstrap 标签页的使用(tab)
    js 循环生成元素,并为元素添加click事件,结果只执行最后一个点击事件
    使用原生js实现一个列表数据展示页面不同的项目状态使整行显示不同颜色。
    【Vue】详解Vue组件系统 目录
    基于TCP与UDP协议的socket通信
    ElementUI三级菜单checkBox全选实现
  • 原文地址:https://www.cnblogs.com/slightFly/p/12073692.html
Copyright © 2011-2022 走看看