vue.js - 如何在计算方法和mapState中分配数组中的动态参数
问题描述
你好,这是我的计算方法:
computed:
mapState({
aftermovie: (state) => state.festival.aftermovies[id]
}),
id: {
set() { return this.$route.params.id}
},
如果我把 state.festival.aftermovies [0] 它工作但如果我尝试在 url 中获取 id,id 是未定义的,请你帮帮我
解决方案
非常感谢是的,确实我们无法在mapState中访问它,这是功能解决方案:
data() {
return {
// -1 because the aftermovie index is 1 and the index in the array starts at 0
id: this.$route.params.id - 1
}
},
computed:
mapState({
aftermovies: (state) => state.festival.aftermovies
}),
最后访问数据
<template>
<div>
<div class="container text-center">
<div>
{{ aftermovies[this.id].id }}
{{ aftermovies[this.id].name }}
{{ aftermovies[this.id].video }}
</div>
</div>
</div>
</template>
推荐阅读
- bash - 试图在 bash 函数中设置状态
- php - 如何从 Google App Engine Standard 上的 CI 应用程序的子目录运行自定义 php 应用程序?
- android - 按地点类型获取所有纬度
- u-sql - 根据 U-SQL 中的配置值过滤记录
- java - 在 Java 记录器中,我如何添加检查以查看它们是否输出某个日志级别的代码
- azure - 具有相同端点的多个 Web api
- java - 如何仅使用 Scanner 读取文件并将每个句子存储在 arrayList 中?
- jquery - JQuery - 如何动态设置一个div的高度等于另一个div的高度
- azure - 获取 Azure Active Directory 中的所有 AppRole 分配
- javascript - 使用 JQuery 将 JSON 上传到基于 Dropwizard 的 API 时出现 HTTP 415 错误