useLink.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { reactive, ref, watch } from 'vue'
  2. import { useRoute, useRouter } from 'vue-router'
  3. export interface ILink {
  4. text: string;
  5. path?: string;
  6. id?: string | number;
  7. url?: string;
  8. }
  9. function useLink() {
  10. const linkList1 = reactive<ILink[]>([
  11. { path: '/article/article_details', id: '123', text: 'VAVA BUY 是如何工作的' },
  12. { path: '/article/article_details', id: '456', text: '为什么选择 VAVA BUY' },
  13. { path: '/article/article_details', id: '3', text: '您所在国家/地区的运费和价格' },
  14. { path: '/article/article_details', id: '4', text: '报名' },
  15. { path: '/article/article_details', id: '5', text: '汇率' },
  16. { path: '/article/article_details', id: '6', text: '不能寄送的物品' },
  17. { path: '/faq', id: '', text: '常见问题' },
  18. { path: '/article/article_details', id: '8', text: '日志' },
  19. { path: '/article/article_details', id: '9', text: '您的隐私权利' },
  20. ])
  21. const linkList2 = reactive<ILink[]>([
  22. { path: '/article/article_details', id: '10', text: '关于 VAVA BUY' },
  23. { path: '/article/article_details', id: '11', text: 'VAVA BUY 评论' },
  24. { path: '/article/article_details', id: '12', text: '新闻与动态' },
  25. { path: '/article/article_details', id: '13', text: 'VAVA BUY.com 的招聘信息' },
  26. { path: '/article/article_details', id: 'abcdefg', text: '与 VAVA BUY 合作' },
  27. { path: '/article/article_details', id: '15', text: '联系我们' },
  28. { path: '/article/article_details', id: '16', text: '网站地图' },
  29. { path: '/article/article_details', id: '17', text: '奖学金' },
  30. { path: '/article/article_details', id: '18', text: '顶级商店' },
  31. ])
  32. const linkList3 = reactive<ILink[]>([
  33. { path: '/article/article_details', id: '19', text: 'Facebook' },
  34. { path: '/article/article_details', id: '20', text: '社交平台' },
  35. { path: '/article/article_details', id: '21', text: 'X(原 Twitter)' },
  36. { url: 'https://www.baidu.com', text: '获取 VAVA BUY 应用程序' },
  37. ])
  38. const route = useRoute()
  39. const router = useRouter()
  40. const currentPath = ref<string>('')
  41. const currentId = ref<string | number | null>(null)
  42. // 获取最新路由信息(路径&id)
  43. watch(
  44. () => route.fullPath,
  45. (newVal, oldVal) => {
  46. console.log('route', route)
  47. currentPath.value = route.path
  48. currentId.value = !!route.query.id ? (route.query.id as (string | number)) : null
  49. },
  50. { immediate: true }
  51. )
  52. // 跳转回调
  53. const handleTextLinkClick = function (linkInfo: ILink, id?: string | number) {
  54. if (!linkInfo || (!linkInfo.path && !linkInfo.url)) return
  55. if (linkInfo.url) {
  56. window.open(linkInfo.url, '_blank')
  57. }
  58. else if (linkInfo.id) {
  59. router.push({
  60. path: linkInfo.path,
  61. query: {
  62. id: linkInfo.id ? linkInfo.id : ''
  63. }
  64. })
  65. }
  66. else {
  67. router.push({ path: linkInfo.path })
  68. }
  69. }
  70. return {
  71. linkList1,
  72. linkList2,
  73. linkList3,
  74. route,
  75. router,
  76. currentPath,
  77. currentId,
  78. handleTextLinkClick
  79. }
  80. }
  81. export default useLink