Vue js条件布局

时间:2018-02-01 03:34:21

标签: javascript vue.js vue-router vuetify.js

我有一个使用vuetify布局的vue js应用程序。主要观点如下:
main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuetify from 'vuetify'
import { App } from './app'
import router from './router'
import store from './store'
import('../node_modules/vuetify/dist/vuetify.min.css')

/* eslint-disable no-new */

Vue.use(Vuetify)

new Vue({
  el: '#app',
  store,
  router,
  render: h => h(App)
})

路由器/ index.js

import Vue from 'vue'
import Router from 'vue-router'
import routes from '../app/routes'

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: routes
})

应用/ index.js

export { default as routes } from './routes'
export { default as vuex } from './vuex'
export { default as App } from './App'

应用/ routes.js

import accounts from './accounts/routes'
import budgets from './budgets/routes'
import transactions from './transactions/routes'

import IndexComponent from './IndexComponent'

var rt = [
  {
    path: '/',
    component: IndexComponent,
    name: 'index'
  }
]

export default [...rt, ...accounts, ...budgets, ...transactions]

app / App.vue (现在基本上是布局)

<template>
  <v-app id="inspire" dark="">
    <v-navigation-drawer
      clipped=""
      fixed=""
      v-model="drawer"
      app="">

      <v-list dense="">

        <v-list-tile :to="{name: 'index', params: {id: 'test'}}">
          <v-list-tile-action>
            <v-icon>home</v-icon>
          </v-list-tile-action>
          <v-list-tile-content>
            <v-list-tile-title>Home</v-list-tile-title>
          </v-list-tile-content>
        </v-list-tile>

        <v-list-tile :to="{name: 'accountsListView', params: {id: 'add'}}">
          <v-list-tile-action>
            <v-icon>dashboard</v-icon>
          </v-list-tile-action>
          <v-list-tile-content>
            <v-list-tile-title>Tester</v-list-tile-title>
          </v-list-tile-content>
        </v-list-tile>

        <v-list-tile @click.stop="dialog = true">
          <v-dialog v-model="dialog" max-width="290">
            <v-card>
              <v-card-title class="headline">Use Google's location service?</v-card-title>
              <v-card-text>Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.</v-card-text>
              <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="green darken-1" flat="flat" @click.native="dialog = false">Disagree</v-btn>
                <v-btn color="green darken-1" flat="flat" @click.native="dialog = false">Agree</v-btn>
              </v-card-actions>
            </v-card>
          </v-dialog>
          <v-list-tile-action>
            <v-icon>settings</v-icon>
          </v-list-tile-action>
          <v-list-tile-content>
            <v-list-tile-title>Settings</v-list-tile-title>
          </v-list-tile-content>
        </v-list-tile>
      </v-list>
    </v-navigation-drawer>
    <v-toolbar app="" fixed="" clipped-left="">
      <v-toolbar-side-icon @click.stop="drawer = !drawer"></v-toolbar-side-icon>
      <v-toolbar-title>Lost Memories</v-toolbar-title>
    </v-toolbar>

    <router-view />

    <v-footer app="" fixed="">
      <span>&copy; 2018</span>
    </v-footer>
  </v-app>
</template>

<script>
export default {
  data: () => ({
    drawer: null,
    dialog: false
  }),
  props: {
    source: String
  }
}
</script>

应用/ IndexComponent.vue

<template>
    <v-content>
      <v-container fluid="" fill-height="">
        <v-layout justify-center="" align-center="">
          <v-tooltip right="">
            <v-btn icon="" large="" :href="source" target="_blank" slot="activator">
              <v-icon large="">code</v-icon>
            </v-btn>
            <span>Source</span>
          </v-tooltip>
        </v-layout>
      </v-container>
    </v-content>
</template>

<script>
export default {
  data: () => ({
  }),
  props: {
    source: String
  }
}
</script>

应用/帐户/ routes.js

import * as components from './components'

export default [
  {
    path: '/accounts',
    component: components.AccountsListView,
    name: 'accountsListView'
  },
  {
    path: '/accounts/create',
    component: components.CreateEditAccounts,
    name: 'createAccounts'
  },
  {
    path: '/accounts/edit',
    component: components.CreateEditAccounts,
    name: 'editAccounts'
  }
]

然后是一些虚拟帐户组件。正如主页上预期的那样,IndexComponent取代route-view并按预期呈现。我目前面临两个主要问题:

  1. 如何使用vue有条件地更改布局?例如,如果用户未经过身份验证,那么我想将他重定向到另一个具有不同布局的视图。如何更换当前布局?这样的架构甚至可能吗?
  2. 在主页上,Home链接以蓝色突出显示(作为有效状态)并且正确无误,但当我访问其他网址/account时,工具中会突出显示这两个链接菜单,我是否错误地设置了路线,还是有其他原因?

1 个答案:

答案 0 :(得分:3)

对于身份验证,我使用vue-router的每路由保护beforeEnter,ref Navigation Guards

Authguard只检查Vuex商店中的有效用户,但您可以使用if..else块中的任何表达式。

{ path: '/tasks', name: 'Tasks', component: Tasks, 
  beforeEnter: (to, from, next) => {
    if (AuthGuard.canActivate()) {
      next()
    } else {
      next({ path: `/login?returnUrl=${to.path}&prompt=Team Tasks` })
    }
  } 
},

注意,如果没有登录,我会将它们带到登录页面,但也会传递一个returnUrl,以便在成功登录后返回到原始请求的页面。

Login.vue组件处理返回如此

created() {
  this.returnUrl = this.$route.query.returnUrl
}
methods: {
  login() {
    // record the user details
    if (this.returnUrl) {
      this.$router.push(this.returnUrl);
    }
  },
}

重新激活链接,在标准的vue导航中使用

<router-link to="/tasks" tag="li" active-class="active"><a>Team Tasks</a></router-link> 

其中active-class属性负责将active(用于引导程序)放在一个链接上。

我不知道你是如何为你的导航做的,或许在vuetify中寻找类似的东西。