Joe*_*uer 1 vue.js vue-router nuxt.js
在我的 Nuxt 应用程序中,我有一个属性列表,单击时我想(/properties/_slug.vue)在用户所在的任何页面前面打开相应的属性页面作为模式。
dribble.com是我正在尝试重新创建的功能的一个确切示例。
如果从 中单击属性链接/,则 url 应该是/properties/slug-of-prop,当我点击关闭按钮或后退按钮时,它应该关闭模式并将 url 返回到单击前的状态。此外,我希望能够访问localhost:3000/properties/slug-of-prop并让它向我显示properties前面带有模态的列表。
我的文件结构是:
/pages
--index.vue
--/properties
----index.vue
----_slug.vue
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这里列出的解决方案:https : //stackoverflow.com/a/60793173/2232797但是,它不会在点击后退按钮时删除模态。此外,我还尝试在此处通过使用中间件添加showModal为我的路由的参数来实现此 vue.js 解决方案,但它正在使用router-view我很难理解它是如何工作的。
这是我的/properties/index.vue样子:
<template>
<section class="container-fluid">
<!-- property cards -->
<div class="info-row scroll-row border-bottom">
<nuxt-link
v-for="property in properties"
:key="property"
:to="`properties/${property.uid}`"
tag="div"
>
{{ property.uid }}
</nuxt-link>
</div>
<div v-if="showModal" class="modal-route">
<div class="modal-content">
<router-view></router-view>
</div>
</div>
</section>
</template>
<script>
import { store } from '@/store/index.js'
export default {
computed: {
showModal() {
return store.showModal
},
},
.....
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出我正确的方向吗?我显然对此很陌生,因此非常感谢任何帮助!
有一种更简单的方法来处理这个问题,而无需大惊小怪的中间件或手动弄乱历史记录。
我在这里创建了一个 CodeSandbox 项目,并试图使其与您的设置尽可能相似:https ://codesandbox.io/s/wizardly-knuth-rolrn 。检查一下,它包含了我将在一个工作示例中解释的所有内容。
您应该做的第一件事是查看Nuxt Child。它基本上是router-view,但专为 Nuxt 设计。
<div class="info-row scroll-row border-bottom"
@click.prevent.stop
>
<nuxt-link
v-for="property in properties"
:key="property.uid"
:to="`/properties/${property.uid}`"
tag="div"
style="text-style: underline"
>{{ property.uid }}</nuxt-link>
</div>
<div v-if="showModal" class="modal-route">
<div class="modal-content">
<nuxt-child></nuxt-child>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这里有几个注意事项:
@click.prevent.stop了取消点击事件的传播,这可以触发卡片侦听器to加上了 /。丢失时,该路由将仅附加到地址栏中已有的任何路由。key到property.uid,因为那应该是独一无二的。对于 nuxt 子级工作来说,一件非常重要的事情:包含子级元素的文件夹必须与父文件名同名。在这个例子中,我使用了index.vue和index/。Index.vue 是父/index路由,里面的所有东西都被认为是可以渲染的子路由。
这为您提供了这样的文件夹结构:
/pages
index.vue
/index
/properties
_slug.vue
Run Code Online (Sandbox Code Playgroud)
接下来改变的是showModal方法。我正在使用this.$route.matched.length以确定是否有子组件。
computed: {
showModal() {
return this.$route.matched.length;
}
}
Run Code Online (Sandbox Code Playgroud)
由于我实现示例侦听器的方式,slug 本身是这里最复杂的部分。但是,我相信您有其他一些处理方法。
<template>
<div ref="cardContainer" style="padding: 20px; border: 1px solid black" >I'm a card for property {{this.$route.params.slug }}</div>
</template>
<script>
export default {
mounted() {
// Attach listeners for handling clicks outside the card, while preventing propagation
// of clicks in the cards
this.$refs.cardContainer.addEventListener('click', this.stopPropagation)
document.body.addEventListener('click', this.closeModal)
},
beforeDestroy() {
// Make sure to cleanup!
this.$refs.cardContainer.removeEventListener('click', this.stopPropagation)
document.body.removeEventListener('click', this.closeModal)
},
methods: {
// Prevent clicking inside the card from triggering the closeModal
stopPropagation(e) {
e.stopPropagation()
},
closeModal() {
// You can also do this.$router.push('/') to preserve the history
this.$router.replace('/')
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1655 次 |
| 最近记录: |