您的当前位置:首页正文

uniapp微信小程序巧用跳转封装鉴权路由

2024-11-26 来源:个人技术集锦

1.这是封装的跳转方法:

import store from "../stores/store";

function Router(type, url, params) {
  const NoLoginPage = [
    。。。。。
  ];
  var queryString = Object.keys(params)
    .map((key) => `${key}=${params[key]}`)
    .join("&");

  if (!NoLoginPage.includes(url)) {
    if (!store.state.user_store.token) {
      store.commit("user_store/setPath", url);
      store.commit("user_store/setParams", params);
      uni.navigateTo({
        url: "/pages/login/login",
      });
      return;
    }
  }
  switch (type) {
    case "switchTab":
      if (queryString) {
        uni.switchTab({
          url: `${url}?${queryString}`,
        });
      } else {
        uni.switchTab({
          url: url,
        });
      }
      break;
    case "navigateTo":
      if (queryString) {
        uni.navigateTo({
          url: `${url}?${queryString}`,
        });
      } else {
        uni.navigateTo({
          url: url,
        });
      }
      break;
    case "redirectTo":
      if (queryString) {
        uni.redirectTo({
          url: `${url}?${queryString}`,
        });
      } else {
        uni.redirectTo({
          url: url,
        });
      }
      break;
    case "switchTab":
      if (queryString) {
        uni.switchTab({
          url: `${url}?${queryString}`,
        });
      } else {
        uni.switchTab({
          url: url,
        });
      }
      break;
  }
}
export default Router;

2.引入main.js函数进行使用

import Router from "./router/routerLogin";
Vue.prototype.$Router = Router;

3.组件中使用

this.$Router(
        "navigateTo",
        "/page_my/。。。。。",
        ""
      );

4.非常完美,完结撒花
 

显示全文