link.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <view @click.stop="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. text = text.replace(/\'/g, '');
  39. text = text.replace(/(\w+)(?=:)/g, function (val) {
  40. return `"${val}"`;
  41. });
  42. text = text.replace(/:\s*([^,{}\s"]+)/g, function (val) {
  43. const arr = val.split(':');
  44. return `:"${arr[1].trim()}"`;
  45. });
  46. try {
  47. text = JSON.parse(text);
  48. } catch (e) {}
  49. }
  50. if (this.append) {
  51. let pathArr = this.$Route.path.split('/');
  52. pathArr.splice(pathArr.length - this.level, this.level);
  53. pathArr = pathArr.join('/');
  54. if (text.constructor === Object) {
  55. if (text.path) {
  56. text.path = pathArr + text.path;
  57. }
  58. } else {
  59. text = pathArr + text;
  60. }
  61. }
  62. return text;
  63. },
  64. gotoPage() {
  65. if (this.stopNavto) {
  66. return true;
  67. }
  68. const type = navType[this.navType];
  69. if (type == null) {
  70. return console.error(` "navType" unknown type \n\n value:${Object.values(navType).join('、')}`);
  71. }
  72. const navInfo = this.formatNav(this.to);
  73. this.$Router[type](navInfo);
  74. },
  75. },
  76. };
  77. </script>