【发布时间】:2021-10-09 09:41:27
【问题描述】:
我有一个带有 TypeScript v.4.3 的 Vue.js v2.6 项目“幕后”。
我的问题:如果在 Vue 组件 中使用 access modifiers 是否有意义,以及如何正确使用它们,我找不到任何可以解决的文档。
我搜索了 Vue.js docs 和 StackOverflow 的文章,没有任何发现。 (Vue.js 文档完全忽略了它们的存在!)
这就是我目前在我的 Vue 组件中使用访问修饰符的方式:
myComponent.vue 的模板部分:
<template>
<div>
<!-- accessing some of the fields of the class here e.g. the getter -->
</div>
</template>
myComponent.vue 的脚本部分:
<script lang="ts">
import { Vue, Component, Prop, Emit } from "vue-property-decorator";
@Component()
export default class MyComponent extends Vue {
@Prop() public readonly myPropertyVariable!: boolean;
public myVariable!: string; // accessed in template
private myInternalVariable!: boolean; // not accessed in template
public get myGetterVariable(): string {
// obviously accessed in template (getter)
}
@Emit()
public change(): void {}
@Action
public doAction(): void {}
public mounted(): void {}
public myMethod(): boolean {
// accessed in template
}
private myInternalMethod(): boolean {
// not accessed in template
}
}
</script>
我希望,这不是一个基于意见的问题。我假设有一个事实可以证实 Vue 组件中访问修饰符的意义。我有一种强烈的、可能是非理性的抗拒将它们全部省略。
顺便说一句:我在 Rider 中编码,访问修饰符可能对 IDE 代码分析有用。
【问题讨论】:
-
相关问题:stackoverflow.com/questions/55339564/…(无答案)
标签: typescript vue.js vuejs2 access-modifiers vue-class-components