|
| 1 | +import breadData from '@/components/breadcrumb/bread-data' |
| 2 | + |
| 3 | +export const state = () => ({ |
| 4 | + breads: [], |
| 5 | +}) |
| 6 | + |
| 7 | +export const mutations = { |
| 8 | + setBreads(state, breads) { |
| 9 | + state.breads = breads |
| 10 | + }, |
| 11 | +} |
| 12 | + |
| 13 | +export const getters = { |
| 14 | + breads(state) { |
| 15 | + return state.breads.filter(item => item.name) |
| 16 | + }, |
| 17 | +} |
| 18 | + |
| 19 | +export const actions = { |
| 20 | + async generateBreadcrumb({ commit, dispatch }, route) { |
| 21 | + const { path: routePath, name, meta } = route |
| 22 | + |
| 23 | + const setBreads = async breadcrumb => { |
| 24 | + const paths = routePath.split('/') |
| 25 | + const allPromise = breadcrumb.map(async (item, index) => { |
| 26 | + const path = paths.slice(0, index + 2).join('/') |
| 27 | + const matchComps = this.$router.getMatchedComponents(path) |
| 28 | + let to = '' |
| 29 | + |
| 30 | + if (item.action) { |
| 31 | + const name = await dispatch(item.action, route) |
| 32 | + item.name = name || '' |
| 33 | + } |
| 34 | + |
| 35 | + if (!item.disabled && matchComps.length) { |
| 36 | + to = path |
| 37 | + } |
| 38 | + |
| 39 | + return { name: item.name, to } |
| 40 | + }) |
| 41 | + |
| 42 | + const breads = await Promise.all(allPromise) |
| 43 | + commit('setBreads', breads) |
| 44 | + } |
| 45 | + |
| 46 | + if (meta && meta.breadcrumb) { |
| 47 | + setBreads(meta.breadcrumb) |
| 48 | + } else { |
| 49 | + const found = breadData.find(item => item.name === name) |
| 50 | + |
| 51 | + if (found) { |
| 52 | + setBreads(found.breadcrumb) |
| 53 | + } |
| 54 | + } |
| 55 | + }, |
| 56 | + async getAccountTypeInfo(_, route) { |
| 57 | + const id = route.params.id |
| 58 | + const result = await this.$axios.get(`https://jsonplaceholder.typicode.com/users/${id}`) |
| 59 | + |
| 60 | + return result.data.name |
| 61 | + }, |
| 62 | + async getUserTypeInfo(_, route) { |
| 63 | + const id = route.params.uid |
| 64 | + const result = await this.$axios.get(`https://jsonplaceholder.typicode.com/users/${id}`) |
| 65 | + |
| 66 | + return result.data.name |
| 67 | + }, |
| 68 | +} |
0 commit comments