IView-admin 在使用的時候
跳轉客戶詳細后,點擊其它頁面,然后再從選項卡進入頁面時,發下控制台 報錯,不能正常打開客戶詳細頁面
[vue-router] Route with name 'customer/detail/:id' does not exist
地址欄的地址變為 http://localhost:8080/ 正確的地址為 http://localhost:8080/customer/detail/150
路由器配置如下
{
path: 'detail/:id',
name: 'customer/detail',
meta: {
title: '客戶詳細',
hideInMenu: true
},
component: () => import('@/view/customer/detail/detail.vue')
}
最后找到原因是,IView-admin 路由跳轉使用的是
turnToPage (name) {
if (name.indexOf('isTurnByHref_') > -1) {
window.open(name.split('_')[1])
return
}
this.$router.push({
name: name
})
},
采用 this.$router.push({name: name}) 來跳轉
在瀏覽器的Local Storage里發現是這樣存儲的
{"name":"customer/detail","path":"/customer/detail/150","meta":{"title":"客戶詳細","hideInMenu":true}}
name 上邊沒有客戶詳細的ID信息,所以跳轉的時候出現了問題。
現將 mian.vue truenToPage 下新增代碼,采用this.$router.push({path: path})方式來跳轉
turnToPagePath (path) {
if (name.indexOf('isTurnByHref_') > -1) {
window.open(name.split('_')[1])
return
}
this.$router.push({
path: path
})
},
然后修改 main.vue handleClick 部分代碼
handleClick (item) {
// this.turnToPage(item.name)
this.turnToPagePath(item.path)
}
問題解決
由此引發了新問題
從列表打開id為150的客戶信息,再從列表打開id為140的客戶信息。從別的頁面點選項卡跳轉到客戶詳細頁面 發現還是進入到 150的客戶信息,而不是最新 140的客戶信息
解決方法,修改 util.js
之前的代碼
export const getNewTagList = (list, newRoute) => {
const { name, path, meta } = newRoute
let newList = [...list]
if (newList.findIndex(item => item.name === name) >= 0) return newList
else newList.push({ name, path, meta })
return newList
}
修改后的代碼
export const getNewTagList = (list, newRoute) => {
const { name, path, meta } = newRoute
let newList = [...list]
let _index = newList.findIndex(item => item.name === name)
if (_index >= 0) {
if (newList[_index].path !== path) { // 如果name已經存在,判斷path值
newList[_index].path = path // 如果不一樣,修改path值
}
return newList
} else newList.push({ name, path, meta })
return newList
}
