如何將物件拼接成get傳值的形式

廖廖依發表於2018-07-26
  1. 傳遞的引數為params物件
export default {
    data () {
        return {
            phone: '18688754577',
            password: '123456'
        }
    },
    computed: {
        params () {
            let params = {}
            params.phone = this.phone
            params.password = this.password
            return params
        }
    },
    methods: {
        concatParams (params) {
            let paramsStr = ''
            for (var k in params) {
                paramsStr += k + '=' + params[k] + '&'
            }
            paramsStr = '?'+ paramsStr.substr(0, paramsStr.length - 1)
            return paramsStr
        }
    },
    created () {
        concatParams (this.params)
    }
}

複製程式碼
  1. 物件的遍歷
for (var k in params) {
    /* k => 指代的是key*/
    /* params[k] => 指代的是value */
}
複製程式碼

相關文章