gpt4 book ai didi

vue.js - 使用 Vuetify 的导航栏中的动态按钮

转载 作者:行者123 更新时间:2023-12-04 16:03:18 32 4
gpt4 key购买 nike

我有个问题...
我的 VueJS 应用程序使用 Vuetify。
我有一个 <v-toolbar> ,在右侧,我想放置一些按钮,这些按钮会根据 <router-view> 中显示的组件而变化,但我无法从 $route 或 $route 访问组件属性以获取绑定(bind)到组件模型的对象和方法。

我想知道是否有任何方法可以从我的主要组件中分配模型。

我已经尝试过“命名路由”,但我不知道在由 <router-view> 管理的组件之间共享属性的方式是什么?并实时更新。

在简历中:

我有一个带有导航栏的应用程序框架,另外在动态内容中我有一个 <router-view> .取决于 <router-view> 中显示的组件,我想在导航栏中看到与该组件对应的按钮,这些按钮可以交互和更改组件的数据或执行方法。

应用程序

<template>
<v-app>
<router-view></router-view>
</v-app>
</template>

<script>
export default {
name: 'App',
data() {
return {

};
}
};
</script>


index.js(路由器)

import Vue from 'vue'
import Router from 'vue-router'
import AppLogin from '@/components/AppLogin'
import Skeleton from '@/components/Skeleton'
import ShoppingCart from '@/components/ShoppingCart'
import ShoppingCartButtons from '@/components/ShoppingCartButtons'
import ProductSelection from '@/components/ProductSelection'
import ProductSelectionButtons from '@/components/ProductSelectionButtons'
import ProductDetail from '@/components/ProductDetail'

Vue.use(Router)

export default new Router({
routes: [
{
path : '/login',
name : 'AppLogin',
component : AppLogin
},
{
path : '/app',
name : 'Skeleton',
component : Skeleton,
children : [{
path : 'shopping-cart',
components : {
navigation : ShoppingCart,
navButtons : ShoppingCartButtons
}
}, {
path: 'product-selection',
name : 'ProductSelection',
components : {
navigation : ProductSelection,
navButtons : ProductSelectionButtons
}
},
{
path: 'product-detail',
name : 'ProductDetail',
components : {
navigation : ProductDetail
},
props : true
}
]
}
]
})


Skeleton.vue

<template>
<v-container fluid>
<v-navigation-drawer
persistent
:mini-variant="miniVariant"
:clipped="true"
v-model="drawer"
enable-resize-watcher
fixed
app
>

<v-list>

<v-list-tile
value="true"
v-for="(item, i) in items"
:key="i"
:to="item.path">

<v-list-tile-action>
<v-icon v-html="item.icon"></v-icon>
</v-list-tile-action>

<v-list-tile-content>
<v-list-tile-title v-text="item.title"></v-list-tile-title>
</v-list-tile-content>
</v-list-tile>
</v-list>
</v-navigation-drawer>

<v-toolbar
app
:clipped-left="clipped"
>
<v-toolbar-side-icon @click.stop="drawer = !drawer">
</v-toolbar-side-icon>

<v-toolbar-title v-text="$route.meta.title"></v-toolbar-title>
<v-spacer></v-spacer>
<router-view name="navButtons"></router-view>

</v-toolbar>

<v-content>
<router-view name="navigation"/>
</v-content>

<v-footer :fixed="true" app>
<p style="text-align : center; width: 100%">&copy; CONASTEC 2018</p>
</v-footer>
</v-container>
</template>

<script>
export default {
data() {
return {
clipped: true,
drawer: false,
fixed: false,
items: [
{
icon: "shopping_cart",
title: "Carrito de Compras",
path : "/app/shopping-cart"
},
{
icon: "attach_money",
title: "Facturas"
},
{
icon: "account_balance_wallet",
title: "Presupuestos"
},
{
icon: "insert_chart",
title: "Informes"
},
{
icon: "local_offer",
title: "Productos"
},
{
icon: "person",
title: "Clientes"
},
{
icon: "layers",
title: "Cuenta"
},
{
icon: "comment",
title: "Comentarios"
},
{
icon: "settings",
title: "Ajustes"
}
],
buttons : [],
miniVariant: false,
right: true,
rightDrawer: false
};
},
name: "Skeleton"
};
</script>


已编辑

我的解决方案是创建一个新的组件工具栏并为左右按钮添加插槽。

<template>
<div>
<v-navigation-drawer persistent :mini-variant="false" :clipped="true" v-model="drawer" enable-resize-watcher fixed app>
<v-list>
<v-list-tile value="true" v-for="(item, i) in items" :key="i" :replace="true" :to="item.path">

<v-list-tile-action>
<v-icon v-html="item.icon"></v-icon>
</v-list-tile-action>

<v-list-tile-content>
<v-list-tile-title v-text="item.title"></v-list-tile-title>
</v-list-tile-content>
</v-list-tile>
</v-list>
</v-navigation-drawer>

<v-toolbar app :clipped-left="true" color="primary" :dark="true" flat>

<v-toolbar-side-icon v-if="showDrawer" @click.stop="drawer = !drawer">
</v-toolbar-side-icon>

<v-toolbar-side-icon v-if="!!back" @click="back">
<v-icon>keyboard_backspace</v-icon>
</v-toolbar-side-icon>

<v-toolbar-title v-text="title" style="font-size: 1.4em"></v-toolbar-title>
<v-spacer></v-spacer>
<v-card-actions>
<slot name="right"></slot>
</v-card-actions>

</v-toolbar>
<v-snackbar
:timeout="5000"
:top="true"
:multi-line="true"
:vertical="true"
v-model="snackbar.show"
>
{{ snackbar.content }}
<v-btn flat color="white" @click.native="snackbar.show = false">Cerrar</v-btn>
</v-snackbar>
</div>
</template>

<script>
export default {
name: 'app-toolbar',
props: ['title','showDrawer', 'back'],
data() {
return {
drawer : false,
items: [{
icon: "shopping_cart",
title: "Carrito de Compras",
path: "/carrito-compras"
}, {
icon: "attach_money",
title: "Facturas",
path: "/documentos-tributarios"
}, {
icon: "account_balance_wallet",
title: "Presupuestos"
}, {
icon: "insert_chart",
title: "Informes"
}, {
icon: "local_offer",
title: "Productos"
}, {
icon: "person",
title: "Clientes"
}, {
icon: "layers",
title: "Cuenta"
}, {
icon: "comment",
title: "Comentarios"
}, {
icon: "settings",
title: "Ajustes"
}]
};
},
computed : {
snackbar() {
return this.$store.getters.snackbar;
}
}
}
</script>


用途是:

 <app-toolbar title="Carrito de Compras" :showDrawer="true">
<template slot="right">
<v-toolbar-side-icon @click="confirm">
<v-icon>monetization_on</v-icon>
</v-toolbar-side-icon>

</template>
</app-toolbar>

最佳答案

我在最近的一个项目中做了和你一样的事情,发现改变结构是解决此类问题的更简单的方法。

我的结构如下:

app.vue:只包含<router-view>没有其他组件

router.js:父路由是一个布局组件,所有包含我的工具栏和其他布局组件的子路由都是它自己的<router-view>接收子路由

前任:

{
path: '/login',
name: 'Login',
component: load('login')
},
{
path: '/',
component: load('main-layout'),
children: [
{
path: '',
name: 'Home Page',
component: load('homePage')
},
{
path: '/settings',
name: 'Settings',
component: load('settings'),
}
]
}

现在在您的主要布局中:
computed: {
showHomeButton () {
if (this.$route.path === '/') {
return true
}
return false
// Repeat for other routes, etc...
},
}

关于vue.js - 使用 Vuetify 的导航栏中的动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49911973/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com