typescript - Vue 上不存在 Nuxt $route
问题描述
嘿嘿,
我有一个奇怪的问题。我有一个带有 Typescript 的 Nuxt 应用程序。在created
钩我console.log
this.$route
。该日志确实有效。以及从路线中读出参数。
但是我在构建应用程序时在控制台中遇到错误:
$route
类型上不存在属性SomeClass
。
这是我的课:
import Vue from'vue'
import Component from 'vue-class-component'
@Component
export default class SomeClass extends Vue {
public id!: string
created() {
console.log('this:', this)
this.id = this.$route.params.id
}
}
工作正常,console.log
并用它的参数显示正确的路线。我怎样才能摆脱这个错误?
这是我的 tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": [
"esnext",
"esnext.asynciterable",
"dom"
],
"esModuleInterop": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"noImplicitAny": false,
"noEmit": true,
"baseUrl": ".",
"paths": {
"~/*": [
"./*"
],
"@/*": [
"./*"
]
},
"types": [
"@types/node",
"@nuxt/vue-app",
"element-ui/types"
]
}
}
解决方案
好的,感谢@aBiscuit,我明白了。这是 tsconfig 中缺少的类型定义。在“类型”下添加@nuxt/types。
这是我的新的和工作的 tsconfig
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": [
"esnext",
"esnext.asynciterable",
"dom"
],
"esModuleInterop": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"noImplicitAny": false,
"noEmit": true,
"baseUrl": ".",
"paths": {
"~/*": [
"./*"
],
"@/*": [
"./*"
]
},
"types": [
"@types/node",
"@nuxt/vue-app",
"element-ui/types"
]
}
}
推荐阅读
- python-3.x - 如何在 pandas 数据框中分隔一个带有 2 个大写字母的字符串和一个带有正则表达式的空格?
- c++ - 我的 main() 文件中不能包含头文件
- python - 在 TPU 上使用 tf.data.Dataset 和 Keras
- typescript - 带有可选参数和剩余参数的打字稿函数重载
- c++ - HRESULT 0x8876086c D3DERR_INVALIDCALL with DirectX9 几乎遵循文档示例
- powershell - 无法使用 Scoop 将 Windows 上的 GitHub CLI 更新到版本 1.5.0
- laravel - 管理应用程序中的设置
- r - R:下标越界,执行停止
- r - 如何随机替换一个值
- c - 查找数据类型中的位数