programing

Vue 라우터 프로그래밍 방식 네비게이션이 작동하지 않음

prostudy 2022. 4. 4. 21:19
반응형

Vue 라우터 프로그래밍 방식 네비게이션이 작동하지 않음

부에루터를 사용하는 구성 요소의 프로그래밍 방식 내비게이션이 작동하지 않음로그인 성공 후 대시보드 페이지로 리디렉션하십시오.하지만 아무 일도 일어나지 않는다...

관련 코드 - 라우터:

import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import Login from '@/components/Login'
import Dashboard from '@/components/Dashboard'

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login,
      beforeEnter: (to, from, next) => {
        if (store.getters.isAuthenticated) {
          // if user is already authenticated,
          // redirect it to the dashboard
          next('/dashboard') // it works
        } else {
          next()
        }
      }
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: Dashboard,
      beforeEnter: (to, from, next) => {
        if (store.getters.isAuthenticated) {
          next()
        } else {
          // if user is not authenticated,
          // redirect it to the login
          next('/login') // it works
        }
      }
    }
  ]
})

구성 요소의 방법:

userLogin () {
  this.authorizeUser({
    name: this.user.name,
    pass: this.user.pass
  }).then(result => {
    console.log('redirected to dashboard')
    this.$router.push('dashboard') // this doesn't work
  })
}

포장하다.json

{
  "name": "xxx",
  "version": "1.0.0",
  "description": "xxx",
  "author": "xxx",
  "private": true,
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "lint": "eslint --ext .js,.vue src",
    "build": "node build/build.js"
  },
  "dependencies": {
    "amazon-cognito-identity-js": "^1.31.0",
    "axios": "^0.17.1",
    "numeral": "^2.0.6",
    "sweetalert2": "^7.4.0",
    "vee-validate": "^2.0.3",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1",
    "vue-slider-component": "^2.4.9",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-eslint": "^8.2.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "eslint": "^4.15.0",
    "eslint-config-standard": "^10.2.1",
    "eslint-friendly-formatter": "^3.0.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-node": "^5.2.0",
    "eslint-plugin-promise": "^3.4.0",
    "eslint-plugin-standard": "^3.0.1",
    "eslint-plugin-vue": "^4.0.0",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.4",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-webpack-plugin": "^2.30.1",
    "node-notifier": "^5.1.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^0.5.8",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.5.2",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^2.9.0",
    "webpack-dev-server": "^2.9.1",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

거기에는 오류가 없지만, 어떤 조치도 취해지지 않는다 - 나는 여전히 로그인 페이지에 머물러 있다.제가 무엇을 빠뜨리고 있나요?

편집:

이게 통한다: 이거.$router.go('대시보드') 내가 이전 버전을 사용하고 있다는 뜻인가?

리디렉션에 대한 접근 방식이 올바르고this.$router.push('dashboard')올바른 접근법이다.그것을 염두에 두고 그리고 우리의 다른 테스트에 기초하여, 당신의 것을 제거하시오.beforeEnter이것이 문제를 일으키고 리디렉션을 중지할 수 있는 체인의 마지막 지점이기 때문에 경로로부터.

너는 왜 그것이 별도로 작동하지 않는지에 대해 그 문제를 분리해야 할 것이다.

다음을 시도해 보십시오.

this.$router.push({ path: '/dashboard' })

적어도 이건 나한테 효과가 있어.

지금 무슨 일이 일어나고 있는 것은 당신이 전화하고 있다는 것이다.this.$router의 안쪽에result => { }그런 맥락으로this그 기능에 상대적이다.아껴야 한다.this외부적인 맥락에서 보면 효과가 있을 겁니다

이것을 놓아라const router = this.$router;전에this.authorizeUser({그 다음에router.push('dashboard');그러면 효과가 있을 거야

참조URL: https://stackoverflow.com/questions/48440236/vue-router-programmatic-navigation-not-works

반응형