vue.js - id动态路由
问题描述
我正在尝试根据 ID 显示内容,我正在尝试从我的菜单中切换 id。我在设置 ID 时遇到问题。channelName 包含具有不同 ID 的所有频道。当我单击一个项目时,我希望显示 ID 的特定内容。
router-link(:to="{name: 'ChannelPaths', params: {id: findId }}" v-for="channel in channelName" :key="channel.id" tag="p")
{
path: "channels/:id/messages",
name: "ChannelPaths",
component: Content,
props: true
}
...mapState({
channelName: (state) => state.channels.fetchChannels,
}),
findId() {
return this.channelName.find(ch => ch.channel_id)
}
API 示例
{
"message_id": "844646e9-648d-4a06-aa1a-f747133601f9",
"description": null,
"timestamp": null,
"email": null,
"channel_id": "d17b7638-d758-4c91-b852-fa1f939e3671",
"channel_name": "Test"
},
fetchChannelMessages({commit,}, id) {
axios.get(`http://localhost:5000/channels/${id}/messages`)
.then((res) => {
commit(SET_CHANNEL_MSG, res.data);
}).catch(e => console.log(e))
},
.content__msg-list--message.d-flex(v-for="msg in messageList", :key="msg.id")
.d-flex.align-items-center.content__msg-left
b-avatar.mr-3(variant="secondary")
p.mb-0 {{ msg.description }}
span#time.mb-0 {{ msg.timestamp }}
解决方案
id 在您正在迭代的对象中可用。
router-link(:to="{name: 'ChannelPaths', params: {id: channel.channel_id }}" v-for="channel in channelName" :key="channel.channel_id" tag="p")
也许是channel.id
和不是channel.channel_id
。我不能从你的例子中看出。
如果您需要名称中的 id,您将使用方法,而不是计算。
findId(channelName) {
return this.channelName.find(ch => ch.channel_name === channelName).channel_id
}
推荐阅读
- spring - 如何在spring security + spring boot中为同一用户禁用多个登录
- python - 无法使用 psycopg2 将日期时间对象传递到 postgresql
- java - 如何使用 JUnit 使用 Mockito 测试 void 方法
- logging - Log4j2:如何在控制台日志中查找日志配置文件是否已定位
- powershell - 安装 Anaconda 后控制台(powershell 和 CMD)中的 CreateFile() 错误
- r - 文章模板不编译
- haskell - 如何在 Haskell 中创建数字列表
- php - 为什么 php 脚本(Gmail API、moometric/GSuiteSignatureManager)只更新 100 个用户的电子邮件签名?
- android - 如何将数据从 recyclerview 单击传递到与我的 recyclerview 放置在同一活动中的片段?
- bash - 使用 bash 脚本更新文件内容