Vue2路由跳轉(zhuǎn)傳參中文問(wèn)題處理方案
1. 問(wèn)題描述
在el-table中的記錄列表中放置了一個(gè) 操作按鈕,點(diǎn)這個(gè)按鈕時(shí)可以新增一個(gè)tab頁(yè)簽,并將通過(guò)路由傳參方式將一些信息傳遞到新打開(kāi)的tab頁(yè)簽中,但發(fā)現(xiàn)傳遞中文參數(shù)時(shí)會(huì)出現(xiàn) Failed to execute 'setRequestHeader' on 'XMLHttpRequest': String contains non ISO-8859-1 code point.的錯(cuò)誤,如下
1.1. 當(dāng)前vue組件
<template> <div class="app-container"> <el-table :data="tableData.records" :key="tableKey" @cell-click="cellClick" size="small" @filter-change="filterChange" @selection-change="onSelectChange" @sort-change="sortChange" border fit row-key="id" ref="table" style="width: 100%;" v-loading="loading"> <el-table-column align="center" type="selection" width="40px" column-key="selectionId" :reserve-selection="true"/> <el-table-column label="模塊名" :show-overflow-tooltip="true" align="center" prop="moduleName" width=""> <template slot-scope="scope"> <span>{{ scope.row.moduleName }}</span> </template> </el-table-column> <!-- ...... --> <el-table-column label="操作" align="center" column-key="operation" class-name="small-padding fixed-width" width="180px"> <template slot-scope="{ row }"> <i @click="handleSetting(row)" class="el-icon-setting table-operation" style="color: #E6A23C" title="設(shè)置" /> </template> </el-table-column> </el-table> </div> </template> <script> export default { name: "ConfigIndex", data() { return { loading: false }; }, methods: { /** 設(shè)置按鈕操作 */ handleSetting(row) { console.log(row) const configId = row.id; const moduleName = row.moduleName; //有中文內(nèi)容,如 "第一個(gè)模塊" const params = { pageNum: 2,moduleName}; //打開(kāi)新的tab頁(yè)面 this.$tab.openPage("[" + moduleName + "]模塊配置", '/dev/settingsIndex/index/' + configId+"/"+moduleName, params); }, } }; </script> <style lang="scss" scoped></style>
1.2. 跳轉(zhuǎn)到的vue組件
<template> <div> <!-- ...... --> </div> </template> <script> import BasicInfoForm from "@/views/config/BasicInfoForm.vue"; import ConfigApi from "@/api/genConfig.js"; export default { name: "SettingsIndex", components: { BasicInfoForm }, data() { return { activeName: "basic", info: { generateType: "0" }, dbConfig: {} }; }, created() { //獲取路由中傳遞的參數(shù) const routeParams = this.$route.params if (routeParams) { this.info.id = routeParams.configId this.info.vueModuleName = routeParams.moduleName } } }; </script>
1.3. 出現(xiàn)的錯(cuò)誤
信息提示
瀏覽器控制臺(tái)打印
xhr.js:126 Uncaught (in promise) TypeError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': String contains non ISO-8859-1 code point. at setRequestHeader (xhr.js:126:1) at Object.forEach (utils.js:238:1) at dispatchXhrRequest (xhr.js:120:1) at new Promise (<anonymous>) at xhrAdapter (xhr.js:12:1) at dispatchRequest (dispatchRequest.js:52:1)
2. 解決方法
原因是在前端跳轉(zhuǎn)頁(yè)面時(shí),url參數(shù)中的內(nèi)容出現(xiàn)了中文。要解決此問(wèn)題必須對(duì)傳遞中文字符的參數(shù)值進(jìn)行編碼,在接收到參數(shù)后再對(duì)其進(jìn)行解碼即可解決。
JS中通過(guò)下面兩個(gè)方法進(jìn)行編碼與解碼操作
- 編碼:encodeURIComponent(str)
- 解碼:decodeURIComponent(str)
2.1. 當(dāng)前vue組件
<template> <div class="app-container"> <el-table :data="tableData.records" :key="tableKey" @cell-click="cellClick" size="small" @filter-change="filterChange" @selection-change="onSelectChange" @sort-change="sortChange" border fit row-key="id" ref="table" style="width: 100%;" v-loading="loading"> <el-table-column align="center" type="selection" width="40px" column-key="selectionId" :reserve-selection="true"/> <el-table-column label="模塊名" :show-overflow-tooltip="true" align="center" prop="moduleName" width=""> <template slot-scope="scope"> <span>{{ scope.row.moduleName }}</span> </template> </el-table-column> <!-- ...... --> <el-table-column label="操作" align="center" column-key="operation" class-name="small-padding fixed-width" width="180px"> <template slot-scope="{ row }"> <i @click="handleSetting(row)" class="el-icon-setting table-operation" style="color: #E6A23C" title="設(shè)置" /> </template> </el-table-column> </el-table> </div> </template> <script> export default { name: "ConfigIndex", data() { return { loading: false }; }, methods: { /** 設(shè)置按鈕操作 */ handleSetting(row) { console.log(row) const configId = row.id; const moduleName = row.moduleName; //有中文內(nèi)容,如 "第一個(gè)模塊" const params = { pageNum: 2,moduleName}; //打開(kāi)新的tab頁(yè)面,并對(duì)URL中的 moduleName 通過(guò) encodeURIComponent(moduleName)進(jìn)行編碼,解決中文問(wèn)題 this.$tab.openPage("[" + moduleName + "]模塊生成配置", '/tool/genSettingsIndex/index/' +configId+"/"+ encodeURIComponent(moduleName), params); }, } }; </script> <style lang="scss" scoped></style>
2.2. 跳轉(zhuǎn)到的vue組件
<template> <div> <!-- ...... --> </div> </template> <script> import BasicInfoForm from "@/views/config/BasicInfoForm.vue"; import ConfigApi from "@/api/genConfig.js"; export default { name: "SettingsIndex", components: { BasicInfoForm }, data() { return { activeName: "basic", info: { generateType: "0" }, dbConfig: {} }; }, created() { console.log("created====") //獲取路由中傳遞的參數(shù) const routeParams = this.$route.params if (routeParams) { this.info.id = routeParams.configId //這里通過(guò) decodeURIComponent(routeParams.moduleName) 對(duì)路由中的moduleName參數(shù)值進(jìn)行解碼,解決中文問(wèn)題 this.info.vueModuleName = decodeURIComponent(routeParams.moduleName) } } }; </script>
以上就是Vue2路由跳轉(zhuǎn)傳參中文問(wèn)題處理方案的詳細(xì)內(nèi)容,更多關(guān)于Vue2路由跳轉(zhuǎn)傳參的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Element Notification通知的實(shí)現(xiàn)示例
這篇文章主要介紹了Element Notification通知的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Element中的Cascader(級(jí)聯(lián)列表)動(dòng)態(tài)加載省\市\(zhòng)區(qū)數(shù)據(jù)的方法
這篇文章主要介紹了Element中的Cascader(級(jí)聯(lián)列表)動(dòng)態(tài)加載省\市\(zhòng)區(qū)數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03vue使用better-scroll實(shí)現(xiàn)下拉刷新、上拉加載
這篇文章主要為大家詳細(xì)介紹了vue使用better-scroll實(shí)現(xiàn)下拉刷新、上拉加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Vue路由重復(fù)點(diǎn)擊報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Vue路由重復(fù)點(diǎn)擊報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04使用ElementUI循環(huán)生成的Form表單添加校驗(yàn)
這篇文章主要介紹了使用ElementUI循環(huán)生成的Form表單添加校驗(yàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue-cli3項(xiàng)目升級(jí)到vue-cli4 的方法總結(jié)
這篇文章主要介紹了vue-cli3項(xiàng)目升級(jí)到vue-cli4 的方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03vue3封裝el-pagination分頁(yè)組件的完整代碼
這篇文章主要介紹了vue3封裝el-pagination分頁(yè)組件的完整代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-02-02springboot?vue接口測(cè)試前端模塊樹(shù)和接口列表
這篇文章主要為大家介紹了springboot?vue接口測(cè)試前端模塊樹(shù)和接口列表,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Monaco-editor 的 JSON Schema 配置及使用介紹
這篇文章主要為大家介紹了Monaco-editor 的 JSON Schema 配置及使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10