javascript - 无法在路线之间导航。VueJS
问题描述
当我单击按钮时,图像将显示给用户:
当我尝试转到下一个图像(路线)时,它什么都不是!但是当我单击上一个按钮时,而不是转到上一个图像,返回画廊
这是我的路线:
const routes = [
{
path: '/',
name: 'Home',
component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue')
},
{
path: '/about',
name: 'About',
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
},
{
path: '/gallery',
name: 'Gallery',
component: () => import(/* webpackChunkName: "gallery" */ '../views/Gallery.vue')
},
{
path: '/gallery/photo/:img',
name: 'Photo',
component: () => import(/* webpackChunkName: "photo" */ '../views/Photo.vue')
}
]
这是我的照片组件,我在其中显示所选图像:
<template>
<div>
<img v-bind:src="img" alt="imagen" />
<br />
<button @click="previous()">Anterior</button>
<button @click="next()" style="margin-left: 10px">Siguiente</button>
</div>
</template>
<script>
export default {
name: "Photo",
computed: {
img: function () {
const url = this.$route.params.img;
return url.replaceAll("%2F", "/").replaceAll("%3F", "?");
},
},
methods: {
previous: function () {
this.$router.go(-1);
},
next: function () {
this.$router.go(1);
},
},
};
</script>
这是我的照片组件,其中显示了所有图像,用户可以选择其中之一:
<template>
<div id="gallery">
<div id="photo" v-for="(img, index) in photos" alt="img" :key="index">
<img v-bind:src="img" /><br />
<router-link :to="{ name: 'Photo', params: { img: img } }">
<button>Visitar</button>
</router-link>
</div>
</div>
</template>
<script>
export default {
name: "Photos",
//https://images.unsplash.com/photo-1517694712202-14dd9538aa97%3Fixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80
data() {
return {
photos: [
"https://images.unsplash.com/photo-1517694712202-14dd9538aa97?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
"https://images.unsplash.com/photo-1590608897129-79da98d15969?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
"https://images.unsplash.com/photo-1457305237443-44c3d5a30b89?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1053&q=80",
"https://images.unsplash.com/photo-1571171637578-41bc2dd41cd2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
"https://images.unsplash.com/photo-1534972195531-d756b9bfa9f2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjE3MzYxfQ&auto=format&fit=crop&w=1050&q=80",
],
};
},
};
</script>
编辑
我做了两种导航方法,但我认为它是错误的......
解决方案
更改以下部分:
<button @click="previous()">Anterior</button>
<button @click="next()" style="margin-left: 10px">Siguiente</button>
到
<button @click="previous">Anterior</button>
<button @click="next" style="margin-left: 10px">Siguiente</button>
删除函数标识符后的括号。
推荐阅读
- flutter - 如何在flutter中实现双向支付网关(接受和退款)?
- windows - Windows 驱动程序如何与硬件通信?
- laravel - 无法使用 phpseclib 生成的密钥从 GitHub 通过 SSH 克隆
- r - 如何重复表格中的行
- node.js - 是否可以在 react-app-rewired 构建过程中从节点包中删除文件?
- python - 如何语法:从函数返回 break 和 continue 语句?
- gremlin - Gremlin - 1 次遍历中的顶点和边缘 Upsert
- node.js - 在 express 中从不同的控制器调用函数
- python - Windows 上的 Python DEAP 和多处理:AttributeError
- reactjs - React Native - 有没有办法在文本输入的文本中嵌入元素?