router-link.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. };
  11. export default {
  12. props: {
  13. to: {
  14. type: [String, Object],
  15. },
  16. stopNavto: {
  17. type: Boolean,
  18. default: false
  19. },
  20. navType: {
  21. type: String,
  22. default: 'push'
  23. },
  24. level: {
  25. type: Number,
  26. default: 1
  27. },
  28. append: {
  29. type: Boolean,
  30. default: false
  31. }
  32. },
  33. methods: {
  34. formatNav(text) {
  35. if (text != null && text.constructor === String) {
  36. text = text.replace(/\'/g, '');
  37. text = text.replace(/(\w+)(?=:)/g, function(val) {
  38. return `"${val}"`;
  39. });
  40. text = text.replace(/:\s*([^,{}\s"]+)/g, function(val) {
  41. const arr = val.split(':');
  42. return `:"${arr[1].trim()}"`;
  43. });
  44. try {
  45. text = JSON.parse(text);
  46. } catch (e) {}
  47. }
  48. if (this.append) {
  49. let pathArr = this.$Route.path.split('/');
  50. pathArr.splice(pathArr.length - this.level, this.level);
  51. pathArr = pathArr.join('/');
  52. if (text.constructor === Object) {
  53. if (text.path) {
  54. text.path = pathArr + text.path;
  55. }
  56. } else {
  57. text = pathArr + text;
  58. }
  59. }
  60. return text;
  61. },
  62. gotoPage() {
  63. if (this.stopNavto) {
  64. return true;
  65. }
  66. const type = navType[this.navType];
  67. if (type == null) {
  68. return console.error(` "navType" unknown type \n\n value:${Object.values(navType).join('、')}`);
  69. }
  70. const navInfo = this.formatNav(this.to);
  71. this.$Router[type](navInfo);
  72. }
  73. }
  74. };
  75. </script>
  76. <style></style>