简体   繁体   English

“TS2339:与汇总捆绑在一起时,类型‘Vue’上不存在属性‘XX’”

[英]“TS2339: Property 'XX' does not exist on type 'Vue'” when bundling with rollup

I have a Vue/TypeScript class that calls a public method on another Vue/TypeScript class.我有一个 Vue/TypeScript class 调用另一个 Vue/TypeScript class 上的公共方法。 It compiles and runs fine with tsc or WebPack.它可以使用 tsc 或 WebPack 编译并运行良好。 But when compiling with rollup and the rollup-plugin-vue, I get an error that Property 'close' does not exist on type 'Vue'.但是当使用 rollup 和 rollup-plugin-vue 进行编译时,我收到一个错误,即Property 'close' does not exist on type 'Vue'.

Repro link: https://repl.it/@orobert91/PropertyDoesNotExistOnTypeVue复制链接: https://repl.it/@orobert91/PropertyDoesNotExistOnTypeVue

Popover.vue:弹出框.vue:

import { Component } from "vue-property-decorator";
import Vue from "vue";

@Component
export default class Popover extends Vue {
    public close() {
        this.isShown = false;
    }
}

DatePicker.vue:日期选择器.vue:

import Popover from "components/commonControls/Popover.vue";
import { Component } from "vue-property-decorator";
import Vue from "vue";

@Component
export default class DatePicker extends Vue {

    public $refs!: {
        popover: Popover
    }

    public get myMethod() {
        this.$refs.popover.close(); //COMPILATION ERROR HERE
    }
}

tsconfig.json configuration file: tsconfig.json配置文件:

{
    "compilerOptions": {
        "noImplicitAny": true,
        "module": "es6",
        "target": "es6",
        "jsx": "react",
        "jsxFactory": "h",
        "strict": true,
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "moduleResolution": "node",
        "declaration": true,
        "declarationDir": "./dist",
        "baseUrl": "./src",
        "outDir": "./dist",
        "plugins": [
            { "transform": "typescript-transform-paths" },
            { "transform": "typescript-transform-paths", "afterDeclarations": true }
        ]
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "dist"
    ]
}

rollup.config.js configuration file: rollup.config.js 配置文件:

import vue from 'rollup-plugin-vue';
import resolve from 'rollup-plugin-node-resolve';
import cleaner from 'rollup-plugin-cleaner';
import copy from 'rollup-plugin-copy';
import ttypescript from 'ttypescript'
import tsPlugin from 'rollup-plugin-typescript2'

export default {
    input: 'src/index.ts',
    output: {
        format: 'esm',
        file: 'dist/index.js'
    },
    external: ['vue'],
    plugins: [
        resolve(),
        tsPlugin({
            typescript: ttypescript
        }),
        vue(),
        cleaner({
            targets: [
                './dist/'
            ]
        }),
        copy({
            targets: [
              { src: 'src/laravel-mix', dest: 'dist/' },
            ]
        })
    ]
};

I think you may be having this issue because you didn't register the Popover component in your Datepicker.vue component我认为您可能会遇到此问题,因为您没有在Datepicker.vue组件中注册Popover组件

Also, another possible reason for the error could be that you are using this snippet below to tell typescript not to throw an error when using this.$refs.popover.此外,错误的另一个可能原因可能是您正在使用下面的这段代码来告诉 typescript 在使用 this.$refs.popover 时不要抛出错误。

 public $refs:: { popover: Popover }

Maybe instead of this.popover.close() , you meant to write this.$refs.popover.close() ?也许你打算写this.$refs.popover.close() this.popover.close()

Try this尝试这个

Datepicker.vue日期选择器.vue

 @Component({ components: { Popover } }) export default class DatePicker extends Vue { public $refs:: { popover. Popover } public get myMethod() { this.$refs.popover;close(); //using refs to access the DOM directly } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM