如何使用路由器在VueJS中进行路由?

时间:2020-09-30 15:52:35

标签: javascript vue.js vue-router

我遵循了VueJS的Okta OAuth教程。我有一个默认页面,该页面在单击“ / about”路由时显示“ App.vue”组件,然后还显示“ About.vue”组件。但是,单击“关于”链接时,我还将在About.vue组件下方看到App.vue组件中的组件。我不确定为什么仍然在“ / about”路线中看到我的App.vue组件。

以下是我的main.js文件:

import Vue from 'vue'
import App from './App.vue'
import About from './About.vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'


import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
import Auth from '@okta/okta-vue'
import VueRouter from 'vue-router'
import cors from 'cors'

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/implicit/callback', component: Auth.handleCallback() },
    { path: '/about', component: About },
  ]
})

Vue.use(Auth, {
  issuer: 'https://dev-REDACTED.okta.com/oauth2/default',
  clientId: 'REDACTED',
  redirectUri: 'http://localhost:8080/implicit/callback', // Handle the response from Okta and store the returned tokens.
  scopes: ['openid', 'profile', 'email'],
  pkce: true 
})


Vue.config.productionTip = false
//Install BootstrapVue
Vue.use(BootstrapVue)
// Optionally install the BootstrapVue icon components plugin
Vue.use(IconsPlugin)

Vue.use(VueRouter)
Vue.use(cors)

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

我的App.vue组件包含以下内容:

<template>
  <div id="app">
    <router-link to="/" tag="button" id='home-button'> Home </router-link>
    <router-link to="/about">About</router-link>
    <button v-if='authenticated' v-on:click='logout' id='logout-button'> Logout </button>
    <button v-else v-on:click='login' id='login-button'> Login </button>
    <router-view/>
    <app-cat-log-home msg="posts"/>   
  </div>
</template>

<script>
import AppCatLogHome from './components/AppCatLogHome.vue'


export default {
  name: 'App',
  components: {  
    AppCatLogHome
  },
   data: function () {
    return {
      authenticated: false
    }
  },
  created () {
    this.isAuthenticated()
  },
  watch: {
    // Everytime the route changes, check for auth status
    '$route': 'isAuthenticated'
  },
  methods: {
    async isAuthenticated () {
      this.authenticated = await this.$auth.isAuthenticated()
    },
    login () {
      this.$auth.loginRedirect('/')
    },
    async logout () {
      await this.$auth.logout()
      await this.isAuthenticated()

      // Navigate back to home
      this.$router.push({ path: '/' })
    }
  }

}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

我的About.vue组件包含:

<template>
  <div id="about">
    <p>Hello this is the About.vue page</p>
  </div>
</template>

<script>
export default {
  name: 'About'
}
</script>

1 个答案:

答案 0 :(得分:1)

<app-cat-log-home>组件位于App.vue main 视图中(根<router-view>所在的位置),因此该组件将在所有视图中显示。

您可以通过创建“主页”视图并将<app-cat-log-home>移入该视图来解决此问题:

<!-- Home.vue -->
<template>
  <div>
    <app-cat-log-home msg="posts"/>   
  </div>
</template>
<!-- App.vue -->
<template>
  <div id="app">
    <router-link to="/" tag="button" id='home-button'> Home </router-link>
    <router-link to="/about">About</router-link>
    <button v-if='authenticated' v-on:click='logout' id='logout-button'> Logout </button>
    <button v-else v-on:click='login' id='login-button'> Login </button>

    <router-view/>

    <!-- Moved into Home.vue -->
    <!-- <app-cat-log-home msg="posts"/> -->
  </div>
</template>

然后,为Home设置默认路由:

// main.js
import Home from '@/views/Home.vue'

const router = new Vuex.Router({
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About },
    //...
  ]
})