6.Vue-路由传参的方式

2020-05-29  本文已影响0人  程序萌
     <div class="examine" @click="insurance(2)">查看详情</div>

特别注意哦, 组件中 获取参数的时候是route.params 而不是router
router 这很重要~~~

methods:{
  insurance(id) {
       //直接调用$router.push 实现携带参数的跳转
        this.$router.push({
          path: `/particulars/${id}`,
        })
}
   {
     path: '/particulars/:id',
     name: 'particulars',
     component: particulars
   }
   this.$route.params.id
   methods:{
  insurance(id) {
       this.$router.push({
          name: 'particulars',
          params: {
            id: id
          }
        })
  }
    {
     path: '/particulars',
     name: 'particulars',
     component: particulars
   }
   this.$route.params.id
methods:{
  insurance(id) {
        this.$router.push({
          path: '/particulars',
          query: {
            id: id
          }
        })
  }
   
   {
     path: '/particulars',
     name: 'particulars',
     component: particulars
   }
   this.$route.query.id

加密参数

npm install --save js-base64
let Base64 = require('js-base64').Base64
<script>
    export default {
        name: "play",
    }
</script>
Base64.encode('4')  // 返回值是"NA==",此时已经将4转换成了"NA=="
let Base64 = require('js-base64').Base64
<script>
    export default {
        name: "playNext",
    }
</script>
Base64.decode('NA==')  // 返回值的4,将加密后的字符串解密成原来的
const Base64 = {
   //加密
    encode(str) {
        return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
            function toSolidBytes(match, p1) {
                return String.fromCharCode('0x' + p1);
            }));
    },
  //解密
    decode(str) {
        // Going backwards: from bytestream, to percent-encoding, to original string.
        return decodeURIComponent(atob(str).split('').map(function (c) {
            return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
        }).join(''));
    }
}
export default Base64
import Base64 from './utils/base64.js' 
Vue.prototype.$Base64 = Base64;
 this.$Base64.encode('4')  // 返回值是"NA==",此时已经将4转换成了"NA=="
 this.$Base64.decode('NA==')  // 返回值的4,将加密后的字符串解密成原来的
上一篇下一篇

猜你喜欢

热点阅读