typescript 提供了类型声明,做静态类型检查,类型就是最好的注释。
<template>
<div id="demo">
<input type="text" v-model="msg" />
<p>msg: {{ msg }}</p>
<p>computedMsg: {{ computedMsg }}</p>
<button @click="greet">Greet</button>
<p>{{ login }}</p>
<button @click="changeState">change state</button>
</div>
</template>
<script lang="ts">
import { Component, Emit, Inject, Model, Prop, Provide, Vue, Watch } from "vue-property-decorator";
import { State, Getter, Action, Mutation, namespace } from "vuex-class";
@Component
export default class Demo extends Vue {
// data
public msg: string = "Hello World";
// props
@Prop() private name!: string;
// store
@namespace("basic").State((state: any) => state.sexMenu) private sexMenu!: any;
@State("basic") private basic!: any;
@namespace("basic").Action("getHouseList") private getHouseList!: any;
@State private login!: boolean;
@Action private toggleLogin!: any;
// watch
@Watch("msg")
public onMsgChanged(val: string, oldVal: string): string {
return val;
}
// mounted
public mounted(): void {
this.greet();
}
// computed
get computedMsg(): string {
return `computed: ${this.msg}`;
}
// methods
public greet(): void {
console.log(`greeting: ${this.msg}`);
}
public changeState(): void {
this.toggleLogin();
}
}
</script>
<style scoped lang="scss">
#demo {
line-height: 24px;
}
</style>