VueRouter this。$ route.query始终为空

时间:2019-05-25 23:54:36

标签: vue.js vue-router

我正在尝试获取查询参数code,但是$route.query始终为空。我已经根据文档使用了功能模式。缺少什么?

路由器:

// use vue-router
import Router from 'vue-router'
Vue.use(Router)

// create router
const router = new Router({
  routes: [
    {
      path: '/',
      component: Home,
      props: (route) => ({ code: route.query.code })
    }
  ]
})

Home.vue

<template>
  <div>
    <Navbar />
      <FlatHeader />
      <v-content>
        <ComingSoon />
        <Changes />
        <EmailSubscribe />   
      </v-content>
    <AuthorizationModal />
  </div> 
</template>

<script>
import AuthorizationModal from './components/AuthorizationModal';
import FlatHeader from './components/FlatHeader';
import ComingSoon from './components/ComingSoon';
import Changes from './components/Changes';
import EmailSubscribe from './components/EmailSubscribe';

export default {
  name: 'Home',
  components: {
    FlatHeader,
    ComingSoon,
    Changes,
    EmailSubscribe,
    AuthorizationModal
  },
  props: {
    code: {
      type: String,
      default: null
    }
  },
  methods: {
  },
  mounted() {
    console.log(this.$route)
  }
}

</script>

$route控制台输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

我通过将Router上的模式设置为“历史”来解决了这个问题。

路由器:

// create router
const router = new Router({
  mode: 'history', // add 'history' mode
  routes: [
    {
      path: '/',
      component: Home,
      props: (route) => ({ code: route.query.code })
    }
  ]
})