编辑页显示标签名

  1. <template>
  2. <Layout>
  3. -- snip --
  4. <div class="form-wrapper">
  5. <FormItem :value="tag.name" field-name="标签名" placeholder="请输入标签名"/>
  6. </div>
  7. -- snip --
  8. </Layout>
  9. </template>
  10. <script lang="ts">
  11. - snip --
  12. @Component({
  13. components: {Button, FormItem}
  14. })
  15. export default class EditLabel extends Vue{
  16. tag?: {id: string, name: string} = undefined
  17. created(){
  18. const id = this.$route.params.id
  19. tagListModel.fetch()
  20. const tags = tagListModel.data
  21. const tag = tags.find(t => t.id === id)
  22. if (tag) {
  23. this.tag = tag;
  24. } else {
  25. this.$router.replace('/404')
  26. }
  27. }
  28. }

实现更新功能

tagListmodel添加update方法

  1. type TagListModel = {
  2. // ...
  3. update: (id: string, name: string) => 'success' | 'not found' | 'duplicated',
  4. }
  5. const tagListModel: TagListModel = {
  6. // ...
  7. update(id, name) {
  8. const ids = this.data.map(item => item.id);
  9. if (ids.indexOf(id) >= 0) {
  10. const tag = this.data.filter(item => item.id === id)[0];
  11. if (tag.name === name) {
  12. return 'duplicated';
  13. } else {
  14. tag.name = name;
  15. this.save();
  16. return 'success';
  17. }
  18. } else {
  19. return 'not found';
  20. }
  21. }
  22. };

EditLabel.vue里监听update:value事件

  1. <template>
  2. //...
  3. <div class="form-wrapper">
  4. <FormItem :value="tag.name"
  5. @update:value="update"
  6. field-name="标签名"
  7. placeholder="请输入标签名"/>
  8. </div>
  9. //...
  10. </template>
  11. <script lang="ts">
  12. // ...
  13. update(value:string) {
  14. if(this.tag){
  15. tagListModel.update(this.tag.id, value)
  16. }
  17. }
  18. }
  19. </script>

FormItem组件里的update:value事件是input时抛出的

  1. <template>
  2. <div>
  3. <label class="formItem">
  4. <span class="name">{{ fieldName }}</span>
  5. <input type="text" :value="value"
  6. @input="onValueChanged($event.target.value)"
  7. :placeholder="placeholder">
  8. </label>
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. // ...
  13. onValueChanged(value:string){
  14. this.$emit('update:value', value)
  15. }
  16. }
  17. </script>

实现删除功能

tagListModel添加remove方法

  1. remove(id) {
  2. let index = -1
  3. for (let i = 0; i < this.data.length; i++) {
  4. if (this.data[i].id === id){
  5. index = i
  6. break
  7. }
  8. }
  9. this.data.splice(index, 1)
  10. this.save()
  11. return true
  12. }

回退按钮

  1. back():void {
  2. this.$router.back()
  3. }

父元素监听不到click事件,需要在根元素抛出click事件

  1. <Icon class="leftIcon" name="left" @click="back"/>
  1. <svg class="icon" @click="$emit('click', $event)">
  2. <use :xlink:href="'#'+name" />
  3. </svg>

idCreator

新建lib目录,新建idCreator文件

  1. let id:number = parseInt(window.localStorage.getItem('_idMax') || '0') || 0
  2. const createId = ():number => {
  3. id++
  4. window.localStorage.setItem('_idMax', id.toString())
  5. return id
  6. }
  7. export default createId