Skip to content

Commit

Permalink
自己封装的全局数据管理 model.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
mcxue committed May 22, 2020
1 parent 3c646d0 commit 7491976
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 57 deletions.
33 changes: 22 additions & 11 deletions src/components/Account/Choice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@
<div class="choice-wrapper">
<div>类别</div>
<ul>
<li class="selected">默认</li>
<li v-for="value in data" :key="value"
:class="{selected:selected===value}"
@click="toggle(value)">{{value}}</li>
</ul>
<button @click="createLabel">新增标签</button>
</div>

</template>

<script lang="ts">
import Vue from 'vue';
import {Component} from 'vue-property-decorator';
import store from '@/store';
@Component({
computed:{
labelList(){
return store.state.labelList
import {Component, Prop} from 'vue-property-decorator';
@Component
export default class Choice extends Vue {
@Prop() readonly data!: string[];
@Prop() readonly selected!: string;
toggle(label: string){
this.$emit('update:selected',label)
}
createLabel(){
const name = window.prompt("请输入标签名");
if(name ===''){
window.alert('标签名不能为空')
}else{
if(this.data){
this.$emit('update:data',[...this.data,name])
}
}
}
})
export default class Choice extends Vue {
}
</script>

Expand Down
7 changes: 5 additions & 2 deletions src/components/Account/Note.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<template>
<label>
{{value}}
<span>备注</span>
<input type="text" v-model="value"
placeholder="在这里添加备注">
Expand All @@ -9,10 +8,14 @@

<script lang="ts">
import Vue from 'vue';
import {Component} from 'vue-property-decorator';
import {Component, Watch} from 'vue-property-decorator';
@Component
export default class Note extends Vue {
@Watch('value')
onNoteChanged(){
this.$emit('update:note',this.value);
}
value = '';
}
</script>
Expand Down
14 changes: 3 additions & 11 deletions src/components/Account/Show.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
<template>
<div class="show">
<div class="text">金额</div>
<div class="number">{{output}}</div>
<div class="number"></div>
</div>
</template>

<script lang="ts">
import Vue from 'vue';
import {Component} from 'vue-property-decorator';
import personStore from '@/store/personStore'
@Component({
// todo
computed:{
output(){
return personStore.output;
}
}
})
@Component
export default class Show extends Vue {
}
</script>
Expand Down
9 changes: 5 additions & 4 deletions src/components/Account/Write.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<button @click="inputContent">7</button>
<button @click="inputContent">8</button>
<button @click="inputContent">9</button>
<button class="ok">确认</button>
<button @click="ok" class="ok">确认</button>
<button @click="inputContent" class="zero">0</button>
<button @click="inputContent">.</button>
</div>
Expand All @@ -27,16 +27,17 @@
remove(){
// todo
personStore.remove();
console.log(personStore.output)
}
empty(){
personStore.empty();
console.log(personStore.output)
}
inputContent(event: MouseEvent){
personStore.inputContent(event);
console.log(personStore.output)
}
ok(){
this.$emit('update:amount',personStore.output);
this.$emit('submit',personStore.output);
}
}
</script>
Expand Down
33 changes: 16 additions & 17 deletions src/components/Type.vue
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
<template>
<ul>
<li :class="{selected: type==='+'}" @click="selectA">支出</li>
<li :class="{selected: type==='-'}" @click="selectB">收入</li>
<li :class="{selected: type==='+'}" @click="toggle('+')">支出</li>
<li :class="{selected: type==='-'}" @click="toggle('-')">收入</li>
</ul>
</template>

<script lang="ts">
import Vue from 'vue';
import {Component} from 'vue-property-decorator';
import {Component, Prop} from 'vue-property-decorator';
@Component
export default class Type extends Vue {
typeList=['+','-'];
type='+';
selectA(){
this.type=this.typeList[0]
}
selectB(){
this.type=this.typeList[1]
@Prop({default:'-'}) readonly type!: string;
toggle(value: string) {
this.$emit('update:type',value)
}
}
</script>

<style lang="scss" scoped>
@import "~@/assets/helper.scss";
ul {
background: $color-sky-blue;
display: flex;
padding-left: 0;
padding-right:0;
padding-right: 0;
> li {
text-align: center;
width: 50%;
Expand All @@ -37,15 +35,16 @@
&.selected {
position: relative;
&::after{
$width:32px;
content:'';
&::after {
$width: 32px;
content: '';
position: absolute;
left:50%;
left: 50%;
bottom: 5px;
background: #ffffff;
height:3px;
width:$width;
height: 3px;
width: $width;
margin-left: -$width/2;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/custom.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
declare module "*.svg" {
const content: string;
export default content;
}

type RecordList = {
type: string;
choice: string;
note: string;
amount: number;
createdAt?: Date;
}
44 changes: 32 additions & 12 deletions src/views/Account.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
<template>
<Layout name="记账" class-prefix="layout">
<Type/>
<Choice/>
<Note/>
<Show @ok="onOutputChange"/>
<Write/>
</Layout>
<Layout name="记账" class-prefix="layout">
<Type :type.sync="record.type"/>
<Choice :selected.sync="record.choice" :data.sync="labelList"/>
<Note :value="record.note" @update:note="onUpdateNote"/>
<Show @ok="onOutputChange"/>
<Write @update:amount="onUpdateAmount" @submit="saveRecord"/>
</Layout>
</template>

<script lang="ts">
import Vue from 'vue';
import {Component} from 'vue-property-decorator';
import {Component, Watch} from 'vue-property-decorator';
import Type from '@/components/Type.vue';
import Choice from '@/components/Account/Choice.vue';
import Note from '@/components/Account/Note.vue';
import Write from '@/components/Account/Write.vue';
import Show from '@/components/Account/Show.vue';
import { model } from './model';
const records = model.fetch();
@Component({
components: {Show, Write, Note, Choice, Type},
},)
})
export default class Account extends Vue {
output='0';
onOutputChange(){
console.log("检测到 output 变化啦")
labelList = ['默认', '', '', '', ''];
record: RecordList = {type: '+',choice:'默认',note: '',amount: 123};
output = '0';
onOutputChange() {
console.log('检测到 output 变化啦');
}
onUpdateNote(note: string) {
this.record.note = note;
}
onUpdateAmount(amount: string) {
this.record.amount = parseFloat(amount);
}
saveRecord(){
this.record.createdAt = new Date;
records.push(model.clone(this.record));
model.save(records);
console.log(records);
}
}
</script>
Expand Down
15 changes: 15 additions & 0 deletions src/views/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const localStorageKeyName = 'recordList';

const model = {
clone(data: RecordList[] | RecordList){
return JSON.parse(JSON.stringify(data))
},
fetch() {
return JSON.parse(window.localStorage.getItem(localStorageKeyName) || '[]') as RecordList[];
},
save(data: RecordList[]) {
window.localStorage.setItem(localStorageKeyName, JSON.stringify(data));
}
};

export {model};

0 comments on commit 7491976

Please sign in to comment.