为什么在.vue中声明的基于ts类的vue组件的静态方法只能在.vue的脚本块中工作?

问题描述 投票:1回答:1

为什么在.vue中声明的基于ts类的vue组件的静态方法只能在.vue的脚本块中工作?

复制步骤

  1. 使用vue-cli3初始化打字稿项目并添加shims-vue.d.ts

declare module "*.vue" {
  import Vue from "vue";
  export default Vue;
}


  1. 在项目中添加[[Comp.vue文件,如下所示
// Comp.vue <template> <div></div> </template> <script lang="ts"> import { Component, Vue, Prop } from "vue-property-decorator"; @Component export default class Comp extends Vue { mounted() { } created(){ } static notWorkFunc(){ //anything } } export function anotherNotWorkFunc(){ //anything } </script>
    使用以下特定代码在项目中添加[[test.ts
  • test.vue]
  • test.vue

    <script lang="ts"> import ComP,{anotherNotWorkFun} from "./Comp.vue"; ComP.notWorkFunc() anotherNotWorkFun() </script> enter image description here

    test.ts

    import ComP,{anotherNotWorkFun} from "./Comp.vue"; ComP.notWorkFunc() anotherNotWorkFun() enter image description here

    如您所见

    。ts中无效

    。vue作品中

    我不知道原因...

    回购是https://github.com/WilkinWendy/vue-ts-problem

    演示代码位于./src/demo中

    FYI

    javascript typescript vue.js vue-cli-3
    1个回答
    0
    投票
    <!-- Comp.vue --> <template> <div></div> </template> <script lang="ts" src="./Comp.ts"></script> <style lang="scss" src="./Comp.scss"></style>

    然后我们执行以下操作:

    // Comp.ts
    import { Component } from "vue-property-decorator";
    
    export const SOME_CONST = "SomeValue";
    
    @Component
    export class Comp extends Vue{
      public static method(): boolean {
        return true;
      }
    }
    
    export default Comp;
    

    这似乎对我们有用,实际上VSCode / Vetur / TypeScript的性能要好得多。另外,当UX用户使用CSS / HTML时,他们不必触摸.ts文件。

    © www.soinside.com 2019 - 2024. All rights reserved.