[英]Why a ts-class-based vue-component's static method, which is declared in .vue, can only work in a .vue's script block?
为什么在.vue 中声明的基于ts-class 的vue-component 的静态方法只能在.vue 的脚本块中工作?
declare module "*.vue" {
import Vue from "vue";
export default 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>
测试.vue
<script lang="ts">
import ComP,{anotherNotWorkFun} from "./Comp.vue";
ComP.notWorkFunc()
anotherNotWorkFun()
</script>
测试文件
import ComP,{anotherNotWorkFun} from "./Comp.vue";
ComP.notWorkFunc()
anotherNotWorkFun()
如你看到的
在.ts 中不起作用
在.vue工作
我不知道原因...
存储库是https://github.com/WilkinWendy/vue-ts-problem 。
演示代码在 ./src/demo
供参考
我认为.vue
文件中的.vue
类组件实际上被编译为 Vue 选项对象,这就是它们的导出方式。 我们在项目中.vue
是在.vue
文件中我们只有模板,在底部我们有对代码隐藏文件的外部引用。 例子:
<!-- 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
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.