plugin.js 968 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. let Plugin = function() {};
  2. Plugin.install = function(vue, router) {
  3. router.$plugin = {
  4. addRoutes: (list, options) => {
  5. if (!options) {
  6. options = {};
  7. }
  8. list.map(e => {
  9. if (!e.component) {
  10. let url = e.viewPath;
  11. if (url) {
  12. if (
  13. /^(http[s]?:\/\/)([0-9a-z.]+)(:[0-9]+)?([/0-9a-z.]+)?(\?[0-9a-z&=]+)?(#[0-9-a-z]+)?/i.test(
  14. e.viewPath
  15. )
  16. ) {
  17. e.meta.iframeUrl = e.viewPath;
  18. e.component = () => import(`@/pages/iframe/index.vue`);
  19. } else {
  20. e.component = () => import(`@/${e.viewPath}`);
  21. }
  22. } else {
  23. e.redirect = '/404';
  24. }
  25. }
  26. });
  27. router.addRoutes([
  28. {
  29. path: '/',
  30. component: resolve => require([`@/pages/layout/index.vue`], resolve),
  31. children: list
  32. }
  33. // {
  34. // path: '*',
  35. // redirect: '/404'
  36. // }
  37. ]);
  38. },
  39. to: url => {
  40. if (router.path != url) {
  41. router.push(url);
  42. }
  43. }
  44. };
  45. };
  46. export default Plugin;