简体   繁体   English

Vue:在助手类中导入路由器并推送路由

[英]Vue: Import router in helper class and push route

I'm using Vue for the first time and try to import my current router instance to a JavaScript class , where I handle the Authentification .我第一次使用Vue并尝试import我当前的router实例importJavaScript class ,在那里我处理Authentification

So this is my router file:所以这是我的router文件:

import Vue from 'vue';
import Router from 'vue-router';
import FirstRoute from '@/components/FirstRoute';
import SecondRoute from '@/components/SecondRoute';

Vue.use(Router);

export default new Router({
  mode: 'history',
  scrollBehavior() {
    return { x: 0, y: 0 };
  },
  routes: [
    {
      path: '/',
      meta: { requiredAuth: false },
      name: 'FirstRoute',
      component: FirstRoute,
    },
    {
      path: '/second',
      meta: { requiredAuth: false },
      name: 'SecondRoute',
      component: SecondRoute,
    },
  ],
});

This is my helper class file, where I try to import and reuse the existing router instance, to push a route in a function :这是我的助手class文件,我尝试在其中导入和重用现有的router实例,以在function push route

import Router from '../router'; /* This is where I import the router instance */

const globalRouter = new Router(); /* Attempt 1 */

class AuthService {
  constructor() {
    console.log(Router); /* This console.log() shows me my router instance with all routes - so it was imported the right way and works */
    const routerInClass = new Router(); /* Attempt 2 */

    this.doSomething();
  }
}

doSomething() {
  const routerInFunction = new Router(); /* Attempt 3 */

  /* Results of my attempts: */
  console.log(globalRouter); /* Result Attempt 1: undefined */
  console.log(routerInClass); /* Result Attempt 2: undefined */
  console.log(routerInFunction); /* Result Attempt 3: undefined */
  console.log(Router); /* Result Attempt 4: undefined */

  /* Use imported router to push a route */
  Router.push({ path: '/SecondRoute' }); /* Not working with attempt 1 to 4 */
}

Use case behind it: I check, if the auth token has expired.它背后的用例:我检查auth token是否已过期。 If true, I save my current href using window.location.href in the localStorage and when logging in again, I redirect to the previous page.如果为真,我会在localStorage使用window.location.href保存我当前的href ,并在再次登录时重定向到上一页。 Now I'm trying to use the Router , because the redirect flickers and I hope it will be smoother with the `Router.现在我正在尝试使用Router ,因为重定向闪烁,我希望使用 `Router.

This are my attempts, which all failed.这是我的尝试,都失败了。 I can log the Router in the constructor , but in my function it is always undefined .我可以在constructor function记录Router ,但在我的function它始终是undefined I can't do the push there.我不能在那里push Any ideas?有任何想法吗?

In your helper file, you don't need to import Vue nor VueRouter在您的帮助文件中,您不需要导入 Vue 或 VueRouter

import router from '../router';

class AuthService {
    constructor() {
        console.log(router);
        this.doSomething();
    }

    doSomething() {
        /* Use imported router to push a route */
        router.push({ path: '/SecondRoute' });
    }
}

EDIT : move the doSomething function in your class编辑:在你的班级中移动 doSomething 函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM