link.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <view @click="gotoPage()"><slot></slot></view>
  3. </template>
  4. <script>
  5. const navType = {
  6. push: 'push',
  7. replace: 'replace',
  8. replaceAll: 'replaceAll',
  9. pushTab: 'pushTab',
  10. back:'back'
  11. };
  12. export default {
  13. props: {
  14. to: {
  15. type: [String, Object],
  16. required: true
  17. },
  18. stopNavto: {
  19. type: Boolean,
  20. default: false,
  21. },
  22. navType: {
  23. type: String,
  24. default: 'push',
  25. },
  26. level: {
  27. type: Number,
  28. default: 1,
  29. },
  30. append: {
  31. type: Boolean,
  32. default: false,
  33. },
  34. },
  35. methods: {
  36. formatNav(text) {
  37. if (text != null && text.constructor === String) {
  38. const keyArray = [];
  39. text = text.replace(/((\w+)|('\s*(\w+)\s*')|("\s*(\w+)\s*"))\s*(?=:)/g, function (val) {
  40. const key = `"${val.trim().replace(/"|'/g, '')}"`;
  41. keyArray.push(key);
  42. return key
  43. });
  44. const removeReg=/('|")/g;
  45. for (let i = 0; i < keyArray.length; i++) {
  46. const key = keyArray[i];
  47. text=text.replace(new RegExp(`${key}\\s*:\\s*('[^']+')`, 'g'),(...args)=>{
  48. const $1=args[1];
  49. return `${key}:"${$1.replace(removeReg,'')}"`
  50. })
  51. }
  52. try {
  53. text=JSON.parse(text);
  54. } catch (error) {}
  55. }
  56. if (this.append) {
  57. let pathArr = this.$Route.path.split('/');
  58. pathArr.splice(pathArr.length - this.level, this.level);
  59. pathArr = pathArr.join('/');
  60. if (text.constructor === Object) {
  61. if (text.path) {
  62. text.path = pathArr + text.path;
  63. }
  64. } else {
  65. text = pathArr + text;
  66. }
  67. }
  68. return text;
  69. },
  70. gotoPage() {
  71. if (this.stopNavto) {
  72. return true;
  73. }
  74. const type = navType[this.navType];
  75. if (type == null) {
  76. return console.error(` "navType" unknown type \n\n value:${Object.values(navType).join('、')}`);
  77. }
  78. const navInfo = this.formatNav(this.to);
  79. this.$Router[type](navInfo);
  80. },
  81. },
  82. };
  83. </script>