navigate.ts 896 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. export function to(path: string) {
  2. if (nextPageIsPrevPage(path)) {
  3. uni.navigateBack();
  4. return;
  5. }
  6. uni.navigateTo({
  7. url: path,
  8. });
  9. }
  10. export function back() {
  11. uni.navigateBack();
  12. }
  13. export function redirect(url: string) {
  14. uni.redirectTo({
  15. url,
  16. });
  17. }
  18. export function reLaunch() {
  19. uni.reLaunch({
  20. url: "/pages/map/map",
  21. });
  22. }
  23. export function nextPageIsPrevPage(path: string): boolean {
  24. const isFull = path[0] === "/";
  25. const pages = getCurrentPages();
  26. const prevPage = pages.length >= 2 ? pages[pages.length - 2] : undefined;
  27. const prevPageQuery = prevPage
  28. ? Object.keys(prevPage.options)
  29. .map((key) => `${key}=${prevPage.options[key]}`)
  30. .join("&")
  31. : "";
  32. if (prevPage) {
  33. return (
  34. `${prevPage.route}${prevPageQuery ? "?" + prevPageQuery : ""}` ===
  35. (isFull ? path.substring(1) : path)
  36. );
  37. }
  38. return false;
  39. }