在我的VueJS项目中,我有一种机制可以根据用户使用Firebase和状态(vuex)的登录来显示内容,如果用户已登录,则使用v-if
显示内容。因此,此刻我可以将NavigationDrawer
放在正确的位置,但不能在正确的位置显示组件。目标是在左侧显示固定的NavigationDrawer
,在右侧显示页面(组件),并使用VueRouter导航到它们。现在,该项目运行良好,但我不知道该如何加载组件,也不知道<router-view>
放在哪里。
当前页面是它:
App.vue
<template>
<v-app>
<v-content v-if="isLoggedIn">
<v-navigation-drawer v-if="isLoggedIn" permanent>
<v-toolbar flat>
<v-list>
<v-list-tile>
<v-list-tile-title class="title">Application</v-list-tile-title>
</v-list-tile>
</v-list>
</v-toolbar>
<v-divider></v-divider>
<v-list dense nav>
<v-list-item v-for="item in items" :key="item.title" link>
<v-list-tile>
<v-list-tile-action>
<v-icon>{{item.icon}}</v-icon>
</v-list-tile-action>
<v-list-tile-content>
<v-list-tile-title>{{item.title}}</v-list-tile-title>
</v-list-tile-content>
</v-list-tile>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-content>
<login v-else />
</v-app>
</template>
<script>
import login from "@/views/Login";
export default {
name: "App",
components: {
login
},
data() {
return {
items: [
{ title: "Dashboard", icon: "dashboard" },
{ title: "Settings", icon: "settings" }
]
};
},
computed: {
isLoggedIn() {
return this.$store.getters.user != null;
}
}
};
</script>
router / index.js
import Login from '@/views/Login'
import Dashboard from '@/views/Dashboard'
import Settings from '@/views/Settings'
import VueRouter from 'vue-router'
import Vue from 'vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Login',
component: Login
},
{
path: '/Settings',
name: 'Settings',
component: Settings
},
{
path: '/Dashboard',
name: 'Dashboard',
component: Dashboard
}
]
export default new VueRouter({
mode: 'history',
routes
})
Settings.vue
<template>
<h1>Settings</h1>
</template>
<script>
export default {
name: "Settings"
};
</script>
Dashboard.vue
<template>
<h1>Dashboard</h1>
</template>
<script>
export default {
name: "Dashboard"
};
</script>
<style>
</style>
您可以尝试将router-view
放入App.vue中>
<template> <v-app> <v-navigation-drawer v-if="isLoggedIn"> ... </v-navigation-drawer> <v-content> <router-view></router-view> </v-content> </v-app> </template> <script> export default { mounted () { if (this.isLoggedIn) { // this is better to have guard in router // https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards this.$router.push('/login') } }, } </script>
注意: