有什么用

  • 在vue3代替vuex的存在
  • 公共数据共享,刷新中心
  • 全局组件

全局数据状态维护

  1. import { useWallet } from "@/hooks/useWallet";
  2. import * as CommitteeAction from "@/contract/committee";
  3. import { reactive } from "vue";
  4. import { toFixed } from "@/utils/format";
  5. const { account } = useWallet();
  6. const myVotes = reactive({
  7. isRuler: false,
  8. votes: 0,
  9. curPercent: 0,
  10. lockedVote: 0,
  11. loading: false
  12. });
  13. export function useVotes() {
  14. /**
  15. * @description: get My vote
  16. * @param {*}
  17. * @return {*}
  18. */
  19. async function fetchMyVote() {
  20. if (!account.value) return;
  21. try {
  22. myVotes.loading = true;
  23. const getUserStakedInfo = await CommitteeAction.getUserStakedInfo(
  24. account.value
  25. );
  26. let myLockedVote = (getUserStakedInfo[3] * 1) / 10 ** 18;
  27. myVotes.votes = (getUserStakedInfo[1] * 1) / 10 ** 18 - myLockedVote;
  28. myVotes.votes = toFixed(myVotes.votes, 3);
  29. myVotes.lockedVote = myLockedVote;
  30. myVotes.isRuler = getUserStakedInfo[0];
  31. myVotes.curPercent = getUserStakedInfo[1] / getUserStakedInfo[2] || 0; //池子中的比例分额计算
  32. } catch (error) {
  33. console.log(error, "fetchMyVote");
  34. } finally {
  35. myVotes.loading = false;
  36. }
  37. }
  38. return {
  39. fetchMyVote,
  40. myVotes
  41. };
  42. }

全局组件引入

全局提示框

  1. import { createApp } from "vue";
  2. import ToastMessage from "@/components/ToastMessage";
  3. import icons from "@/plugins/icons";
  4. // let index = 0; //当前索引
  5. let queue = []; //message队列
  6. const defaultTop = 10;
  7. export function Message(option) {
  8. const dom = document.createElement("div");
  9. const id = `message-${queue.length}`;
  10. document.body.appendChild(dom);
  11. if (typeof option === "object") {
  12. option = {
  13. modelValue: true,
  14. ...option
  15. };
  16. } else {
  17. option = {
  18. modelValue: true,
  19. content: option
  20. };
  21. }
  22. const app = createApp(ToastMessage, {
  23. destroy: () => {
  24. // index--;
  25. queue = queue.slice(1);
  26. const el = dom.firstElementChild;
  27. if (el) {
  28. el.style.transform = `translateX(${2 * defaultTop}px)`;
  29. el.style.opacity = "0";
  30. setTimeout(() => {
  31. app.unmount(dom);
  32. if (document.body.contains(dom)) document.body.removeChild(dom);
  33. }, 300);
  34. }
  35. },
  36. ...option
  37. });
  38. const vm = app.use(icons).mount(dom);
  39. vm.$el.setAttribute("id", id);
  40. setTimeout(() => {
  41. vm.$el.style.marginTop = `${defaultTop}px`;
  42. });
  43. //存在其他message,该index之前的所有message往下移动
  44. if (queue.length > 0) {
  45. moveDownMessages(queue.length);
  46. }
  47. // index++;
  48. queue.push(vm.$el);
  49. function moveDownMessages(end) {
  50. const height = 120;
  51. const margin = 15;
  52. for (let index = 0; index < end; index++) {
  53. const el = queue[index];
  54. const distance = (height + margin) * (end - index) + defaultTop;
  55. setTimeout(() => {
  56. el.style.marginTop = `${distance}px`;
  57. });
  58. }
  59. }
  60. }
  61. Message.success = function(content) {
  62. Message({ type: "success", content });
  63. };
  64. Message.warning = function(content) {
  65. Message({ type: "warning", content });
  66. };
  67. Message.error = function(content) {
  68. Message({ type: "error", content });
  69. };

image.png

顶部警告框

  1. import { ref } from "vue";
  2. export const warningVisible = ref(false);
  3. export const warningContent = ref("");
  4. export function Warning(content) {
  5. if (warningVisible.value) return;
  6. if (warningContent.value === content) return;
  7. warningContent.value = content;
  8. warningVisible.value = true;
  9. }
  10. Warning.close = function() {
  11. warningVisible.value = false;
  12. warningContent.value = "";
  13. };

image.png