1. 程式人生 > >(九)vue開發

(九)vue開發

使用路由獲取頁面引數

  1. 在路由中設定path:
{
    path: '/detail/:id/',
    name: 'detail',
    component: detail,
    meta: {
        title: '詳情'
    }
}

獲取引數

 let id = this.$route.params.id
let id = this.$route.query.id

這樣即使取不到引數,頁面也不會報錯

使用js獲取頁面引數
如果是在普通js檔案中,想獲取url後面的引數,可以新建一個工具類,utils.js:

/* eslint-disable */
export default{ getUrlKey: function (name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null } }

在其他需要獲取引數的js中引入

import Vue from 'vue'
import utils from '../../assets/scripts/utils'
// Vue.prototype.$utils = utils // main.js中全域性引入 let id = utils.getUrlKey('id') console.log()