You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Framework7是否有类似Vue router.beforeEach()的方法用于路由校验用户Token?

Hey there! Great questions about Framework7 routing hooks and token validation. Let's break this down clearly:

1. Framework7中类似Vue router.beforeEach()的方法

Absolutely! Framework7 provides an equivalent routing hook through the route:before event. This event fires right before a route transition starts (but before the new page is loaded/shown), which matches the core behavior of Vue's router.beforeEach().

How to use it:

For vanilla Framework7:

const app = new Framework7({
  // Your app config here
  on: {
    routeBefore(route) {
      // The `route` object contains all info about the upcoming route
      console.log('About to navigate to:', route.url);
      // Add your pre-navigation logic here
    }
  }
});

For Framework7-Vue:

You can listen to the event directly in your Vue components or app entry file:

export default {
  mounted() {
    this.$f7.on('routeBefore', (route) => {
      // Handle pre-navigation checks here
    });
  }
}
2. Implementing Token Validation on Route Navigation

Using the route:before event, you can easily add token validation logic that runs every time the user navigates between routes. Here's a step-by-step implementation:

Core Logic:

  1. Define routes that don't need validation (like login/register pages)
  2. Check if the current route requires validation
  3. Verify the user's token (existence, expiration, etc.)
  4. If the token is invalid, redirect to login and block the original navigation
  5. If valid, allow the navigation to proceed

Example Code (Vanilla Framework7):

const app = new Framework7({
  on: {
    routeBefore(route) {
      // List of routes that don't need token checks
      const exemptRoutes = ['/login/', '/public/'];
      
      // Skip validation for exempt routes
      if (exemptRoutes.includes(route.url)) return true;

      // Get token from local storage (adjust storage method as needed)
      const userToken = localStorage.getItem('authToken');
      
      // Validate token (customize this logic for your token type)
      if (!userToken || isTokenExpired(userToken)) {
        // Redirect to login page
        app.views.main.router.navigate('/login/');
        // Return false to block the original route navigation
        return false;
      }

      // Token is valid, allow navigation
      return true;
    }
  }
});

// Helper function to check if JWT token is expired
function isTokenExpired(token) {
  if (!token) return true;
  // Parse JWT payload (adjust if using non-JWT tokens)
  const payload = JSON.parse(atob(token.split('.')[1]));
  // Check if expiration timestamp is in the past
  return payload.exp * 1000 < Date.now();
}

Framework7-Vue Version:

export default {
  mounted() {
    this.$f7.on('routeBefore', (route) => {
      const exemptRoutes = ['/login/', '/public/'];
      if (exemptRoutes.includes(route.url)) return true;

      const userToken = localStorage.getItem('authToken');
      if (!userToken || this.isTokenExpired(userToken)) {
        this.$f7router.navigate('/login/');
        return false;
      }
      return true;
    });
  },
  methods: {
    isTokenExpired(token) {
      if (!token) return true;
      const payload = JSON.parse(atob(token.split('.')[1]));
      return payload.exp * 1000 < Date.now();
    }
  }
}

A quick note: Returning false from the routeBefore callback will cancel the original navigation, so make sure to do that when redirecting to login to avoid conflicting page transitions.

内容的提问来源于stack exchange,提问作者Igor Bezlepkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:47:56