1. 将Money组件由JS改为TS

@Component里添加components

  1. <script lang="ts">
  2. import Vue from 'vue';
  3. import NumberPad from '@/components/Money/NumberPad.vue';
  4. import Types from '@/components/Money/Types.vue';
  5. import Notes from '@/components/Money/Notes.vue';
  6. import Tags from '@/components/Money/Tags.vue';
  7. import {Component} from 'vue-property-decorator';
  8. @Component({
  9. components: {Tags, Notes, Types, NumberPad}
  10. })
  11. export default class Money extends Vue {
  12. tags = ['衣', '食', '住', '行', '彩票'];
  13. }
  14. </script>

2. 获取Tag组件的选中的标签

Tag组件

  1. <script lang='ts'>
  2. // ...
  3. toggle(tag: string) {
  4. const index = this.selectedTags.indexOf(tag);
  5. if (index >= 0) {
  6. this.selectedTags.splice(index);
  7. } else {
  8. this.selectedTags.push(tag);
  9. }
  10. this.$emit('update:value', this.selectedTags);
  11. }
  12. // ...
  13. </script>

Money组件

  1. <template>
  2. <Layout class-prefix="layout">
  3. <NumberPad/>
  4. <Types/>
  5. <Notes/>
  6. <Tags :data-source.sync="tags" @update:value="onUpdateTags"/>
  7. </Layout>
  8. </template>
  9. <script lang="ts">
  10. // ...
  11. @Component({
  12. components: {Tags, Notes, Types, NumberPad}
  13. })
  14. export default class Money extends Vue {
  15. tags = ['衣', '食', '住', '行', '彩票'];
  16. // vue会自动把自定义事件的值作为第一个参数传给监听事件的method
  17. onUpdateTags(value:string){
  18. console.log(value);
  19. }
  20. }
  21. </script>

3. 获取Note组件的Notes

使用@Watch监听value的变化

  1. <script lang="ts">
  2. import Vue from 'vue'
  3. import {Component, Watch} from 'vue-property-decorator';
  4. @Component
  5. export default class Notes extends Vue{
  6. value = ''
  7. @Watch('value')
  8. onValueChanged(value:string, oldValue:string){
  9. this.$emit('update:value', value)
  10. }
  11. }
  12. </script>

Money组件监听事件

  1. <template>
  2. <Layout class-prefix="layout">
  3. <NumberPad/>
  4. <Types/>
  5. <Notes @update:value="onUpdateNotes"/>
  6. <Tags :data-source.sync="tags" @update:value="onUpdateTags"/>
  7. </Layout>
  8. </template>
  9. <script lang="ts">
  10. // ...
  11. onUpdateNotes(value:string){
  12. console.log(value);
  13. }
  14. }
  15. </script>

4. 获取Type组件的type

  1. <script lang="ts">
  2. //...
  3. @Watch('type')
  4. onTypeChanged(value:string){
  5. this.$emit('update:value', value)
  6. }
  7. }
  8. </script>

@Watch下面的函数名可以任意取,叫xxx都行

5. 获取NumberPad组件的值

  1. <script lang="ts">
  2. // ...
  3. ok() {
  4. this.$emit('update:value', this.output)
  5. }
  6. }
  7. </script>

6. 将数据收集到一起

声明Record类型

  1. type Record = {
  2. tags: string[],
  3. notes: string,
  4. type: string,
  5. amount: number,
  6. createAt?: Date
  7. }
  8. record:Record = {
  9. tags: [], notes: '', type:'-', amount: 0
  10. }

7. 存储到LocalStorage

  • record需要深拷贝,否则每条数据都一样

    1. saveRecord(){
    2. const record2:Record = JSON.parse(JSON.stringify(this.record))
    3. record2.createAt = new Date()
    4. this.recordList.push(record2)
    5. }
    6. @Watch('recordList')
    7. onRecordListChanged(){
    8. window.localStorage.setItem('recordList', JSON.stringify(this.recordList))
    9. }

    程序加载时就读取localStorage

    1. recordList:Record[] = JSON.parse(window.localStorage.getItem('recordList') || '[]')