输入一个路由,输出一个匹配到的路由数组

  1. export function urlToList(url) {
  2. if(typeof url === 'object') return [];
  3. const array = (url || '').split('/').filter(Boolean);
  4. return array.map((item, index) => {
  5. const path = array.slice(0, index + 1).join('/');
  6. return `/${path}`;
  7. });
  8. }

image.png

pathname

  1. urlToList(pathname)