【发布时间】:2017-06-16 21:17:44
【问题描述】:
我正在尝试使用模态创建路由,当您使用路由器链接访问此路由器路径时,当前页面上方会出现一个模态,或者如果我直接从 url 访问,则会出现上面带有模态的索引。
例如:我在http://localhost/profile/1,然后点击侧边栏的创建团队,网址更改为http://localhost/team/create,但模态后面的页面仍然是http://localhost/profile/1。
这是我正在尝试的代码:
路由器:
Vue.component('modal', Modal);
export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'Hello',
component: require('@/components/Hello'),
meta: { auth: false }
},
{
path: '/team',
name: 'team',
component: require('@/components/team/Index'),
meta: { auth: true },
},
{
path: '/team/create',
name: 'CreateTeam',
components: {
b: CreateTeam
},
meta: { auth: true }
},
]
})
App.vue
<template>
<router-view></router-view>
<!-- This is the "MODAL" router-view -->
<router-view name="b"></router-view>
</template>
Modal.vue
<template>
<div class="modal">
<slot name="body"></slot>
<button type="button" @click="$emit('close')">×</button>
</div>
</template>
创建团队.vue
<template>
<modal @close="vm.$router.go(-1)">
<div slot="body" class="col-md-12">
<!-- Form here -->
</div>
</modal>
</template>
一切正常,而不是当我转到 /create/team 时,模态后面是空的
【问题讨论】:
-
在这种情况下,您的模式不应该是一条路线,它应该是与您的
router-view相同的组件的一个元素。当您需要显示它时,使用事件或状态管理从该组件触发它,甚至通过router-view将函数传递给需要触发它的组件。 -
我明白,但在这种情况下,我想在任何组件中打开,如“全局模式”。如果我确实喜欢你说所有组件都必须具有模态组件的代码和不同的路径,例如:配置文件/模态,团队/模态“我想从任何地方打开团队/创建
-
不,我不是这个意思。它应该存在于 App.vue 中,它不应该是路由,并且您的 是 路由的组件应该触发它。
-
当然。所有组件都可以访问
$router。 -
如果你想让后退按钮正常工作并关闭模式,那么最好使用路由器的解决方案,并且会更“干净”
标签: vue.js