【发布时间】:2020-12-07 23:03:26
【问题描述】:
我有一个 API 响应这样的 JSON:
{
"Thriller": "Thriller books",
"Biographical": "Biographical books",
"Romance": "Romance books"
}
我想最终得到这样的路线:
http://example.com/thriller
http://example.com/biographical
http://example.com/romance
本质上,API 应该规定可以查看哪些类型的书籍。之后Books 组件将显示有关特定书籍类型的通用数据。我的路由器配置到此为止:
import Vue from 'vue';
import Router from 'vue-router';
import Home from './views/Home.vue';
import Books from './views/Books.vue';
Vue.use(Router);
var books: Record<string, string> = await fetch('http://example.com/api/available-books')
.then((response) => response.json())
.then((data) => {
return data;
});
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: books.map((shortname: string, fullname: string) => ({ component: Books, path: `/${shortname}`)),
});
但是 TS linter 说map() 有两个问题:行尾的TS1005: comma expected 和TS2349: Cannot invoke an expression - type String has no invocation signature(大致翻译)。
我不明白我应该做什么。也许有更好的方法来做我正在尝试的事情?我是 SPA 方面的新手。
【问题讨论】:
-
错误信息到底是什么?当您记录它时,来自 response.json() 的数据是否以数组的形式出现?
-
@altruios 有两个错误:
TS1005: comma expected在行尾和TS2349: Cannot invoke an expression - type String has no invocation signature(粗略翻译)
标签: typescript vue.js vue-router