typescript - Vue CLI 3 typescript - 类型 'x' 上不存在属性 'Vue'

标签 typescript vue.js vue-cli-3

我有一个毫无意义的奇怪错误。意思是 isGenreVue 类型上不存在,但是 ìsGenre 实际上是一个计算属性

<script lang="ts">
import Vue from 'vue'

import { WIDTH_TWO_ICONS } from '@/const/tables-style'

export default Vue.extend({
    props: {
        list: Array,
        listType: String
    },
    data: () => ({ WIDTH_TWO_ICONS }),
    computed: {
        isGenre(): boolean {
            return this.listType === 'genre'
        },
        isMood(): boolean {
            return this.listType === 'mood'
        }
    },
    methods: {
        routerLink(paramsId: number): object {
            return { name: 'GenreOrMoodEdit', params: { id: paramsId }, query: { type: this.isGenre ? 'genre' : 'mood' } }
        }
    }
})
</script>

错误

59:93 Property 'isGenre' does not exist on type 'Vue'.
    57 |     methods: {
    58 |         routerLink(paramsId: number): object {
  > 59 |             return { name: 'GenreOrMoodEdit', params: { id: paramsId }, query: { type: this.isGenre ? 'genre' : 'mood' } }
       |                                                                                             ^
    60 |         }
    61 |     }
    62 | })

Main.ts

declare module 'vue/types/vue' {
    // Global properties can be declared
    // on the `VueConstructor` interface
    interface VueConstructor {
        $axios: any
        router: any
    }
    interface Vue {
        $axios: any
        router: any
    }
}

import Vue from 'vue'
import App from './App.vue'
import router from '@/router'
import store from '@/store'
import './registerServiceWorker'
import '@/plugins'
import '@/directives'

import '@/sass/main.sass'

Vue.config.productionTip = false

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')

Tsconfig.js

{
    "compilerOptions": {
        "target": "esnext",
        "module": "esnext",
        "strict": true,
        "jsx": "preserve",
        "importHelpers": true,
        "moduleResolution": "node",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "strictNullChecks": false,
        "baseUrl": ".",
        "types": ["node", "mocha", "chai"],
        "paths": {
            "@/*": ["src/*"]
        },
        "lib": ["esnext", "dom", "dom.iterable", "scripthost"]
    },
    "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx"],
    "exclude": ["node_modules"],
    "files": ["src/interfaces/*"]
}

Tslint.js

{
    "defaultSeverity": "warning",
    "extends": ["tslint:recommended"],
    "linterOptions": {
        "exclude": ["node_modules/**", "./src/assets/icons/*"]
    },
    "rules": {
        "quotemark": [true, "single", "avoid-escape"],
        "arrow-parens": false,
        "semicolon": false,
        "object-literal-sort-keys": false,
        "max-line-length": [true, 160],
        "ordered-imports": false,
        "interface-name": false,
        "trailing-comma": false,
        "no-unused-expression": true,
        "object-literal-shorthand": false,
        "curly": false,
        "no-console": false
    }
}

最佳答案

问题是您正在定义 list Prop 作为 Array .

ArrayArrayConstructor 的别名类型。

arrayconstructor

ArrayConstructor 的构造函数返回 Array<any> ,这不同于 ArrayConstructor .

解决方法:

list 创建一个接口(interface)属性(property)。

即。

list将是 Item 的数组.

interface Item { ... }

Array到返回接口(interface)的函数。

props: {
  list: Array as () => Item[],
}

关于typescript - Vue CLI 3 typescript - 类型 'x' 上不存在属性 'Vue',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52609563/

相关文章:

typescript - 我想使用 typescript 将 mikroConfig 初始化为 MikroORM,但我收到此错误消息

javascript - 这个使用 "as"的 Typescript 显式类型转换到底在做什么?

typescript - vscode/eslint 提示解析错误 : Only declares and type imports are allowed inside declare module

javascript - 自动图像裁剪 JavaScript 工具建议

javascript - 将子组件绑定(bind)到组件数据导致 Vue.js

vue.js - Vue 客户端 : Cannot find module '../package.json' error after npm install

javascript - 如何使用 Visual studio 2019 Typescript 使 React 对象不再需要任何声明

javascript - 在 v-for 中操作 v-if 的正确方法

vue.js - 构建新的 Vue CLI 3 项目时,如何修复 terser-webpack-plugin 中的 'TypeError: Cannot read property ' minify' of undefined'?

vue.js - vuejs vue-cli 如何使用console.log 而不会出现任何错误