zoukankan      html  css  js  c++  java
  • Vue.js

    computed如何传参

      computed: {
        url() {
          return function (param) {
            return xxx + param;
          };
        },
      }
    //使用方法 <a :href="url(param)">删除</a> 
    

    关于路由传参刷新页面后数据丢失的解决方案(name+params)

    //about.vue
    <template>
      <div class="about">
        <h1>This is an about page</h1>
        <el-button type="primary" icon="el-icon-share" @click="push"></el-button>
      </div>
    </template>
    <script>
    export default {
        methods:{
          push(){
            this.$router.push({name:'paramsPush',params:{name:'hanpi',age:'22'}})
          }
        }
    }
    </script>
    
    //paramsPush.vue
    <template>
      <div>
        <el-tag type="info">{{paramf}}</el-tag>
        <el-tag type="warning">{{params}}</el-tag>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          paramf: this.$route.params.name,
          params: this.$route.params.age
        };
      }
    };
    </script>
    

    上述两个组件,点击about.vue中的按钮跳转到paramsPush.vue,tag标签可以显示通过params传递的name和age,但是刷新的话页面数据就会丢失

    解决方案一:
    //router规则配置
    //router/index.js
      {
        path: '/paramsPush/:name/:age',
        name: 'paramsPush',
        component: () => import('../views/paramsPush.vue')
      },
    //直接调用$router.push 实现携带参数的跳转
      this.$router.push({
        path: `/describe/${name}/${age}`,
      })
    //取值
      this.$route.params.xxx
    
    解决方案二:
    //使用path+query传参来替代
    //about.vue
    <script>
    export default {
        methods:{
          push(){
            // this.$router.push({name:'paramsPush',params:{name:'hanpi',age:'22'}})
            this.$router.push({path:'/paramsPush',query:{name:'hanpi',age:'22'}})
          }
        }
    }
    </script>
    
    //paramsPush.vue
    <script>
    export default {
      data() {
        return {
        //   paramf: this.$route.params.name,
          paramf: this.$route.query.name,
        //   params: this.$route.params.age,
          params: this.$route.query.age
        };
      }
    };
    </script>
    
    解决方案三:
    //使用localStorage存在本地
    //paramsPush.vue
    <script>
    export default {
      data() {
        return {
          paramf: this.$route.params.name,
          //   paramf: this.$route.query.name,
          params: this.$route.params.age
          //   params: this.$route.query.age
        };
      },
      created() {
        let paramsData = JSON.parse(localStorage.getItem("params"));
        window.console.log(paramsData)
        if (paramsData) {
          this.paramf = paramsData.name;
          this.params = paramsData.age;
        } else {
          localStorage.setItem("params", JSON.stringify(this.$route.params));
        }
      },
      destroyed() {
        localStorage.removeItem("params");
      }
    };
    </script>
    
  • 相关阅读:
    计算机视觉的常用测试数据集和源码
    Demo:基于 Flink SQL 构建流式应用
    102万行代码,1270 个问题,Flink 新版发布了什么?
    bilibili 实时平台的架构与实践
    阿里云2020上云采购季来啦!降本增效1亿补贴!
    阿里云助力宁波市教育局“甬上云校”停课不停学
    Quick BI新版本功能解读系列之-V3.5
    百万TPS高吞吐、秒级低延迟,阿里​搜索离线平台如何实现?
    五分钟学会使用 go modules(含在家办公使用技巧)
    OAM 深入解读:OAM 为云原生应用带来哪些价值?
  • 原文地址:https://www.cnblogs.com/chyshy/p/13084655.html
Copyright © 2011-2022 走看看