vue-router beforeEnter guard не работает при изменении URL - PullRequest
0 голосов
/ 30 августа 2018

У меня есть два пути "# / inv /: invId" и "# / survey". Первый хранит invId в локальном хранилище, чтобы второй мог его получить. Когда я перехожу к # / inv / 123, он переходит к # / survey, и этот код запускается. Затем, если я изменю URL в браузере на # / inv / 129, запускается / inv guard, обновляет localalstorage, сбрасывает переменные состояния и пытается перенаправить на # / survey, но он не вызывается, console.log не срабатывает , Вы видите, что я делаю не так? Или есть другой способ сделать это? В конце я хочу, чтобы идентификатор не был в URL после загрузки опроса.

{
  path: '/inv/:invId',
  name: 'SurveyInvitation',
  beforeEnter: (to, from, next) => {
    if (parseInt(to.params.invId) > 0) {
      localStorage.setItem('invId', to.params.invId)
      state.survey = {}
      state.isValidSurvey = false
      console.log('inv before enter, invId > 0', to.params.invId)
      next('/survey');
    } else {
      console.log('inv before enter, error')
      next('/error')
    }
  }
},
{
  path: '/survey',
  name: 'Survey',
  component: Survey,
  beforeEnter: (to, from, next) => {
    console.log('/survey before enter', localStorage.getItem('invId')) // not called on url change
    let invId = localStorage.getItem('invId')

    if (!parseInt(invId) > 0) { next('/error') }
    state.loadSurvey(invId)
      .then(function () {
        if (state.isValidSurvey) { next() } else { next('/error') }
      })
      .catch(function (err) {
        console.log('not loaded', err)
        next()
      })
  }
}
...