欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vue输入框添加\删除标签

程序员文章站 2022-06-20 10:56:55
...
<!--
 * @Author: your name
 * @Date: 2020-08-09 17:55:34
 * @LastEditTime: 2020-08-09 17:56:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \realworld-nuxt\pages\edit.vue
-->
<style lang="less" scoped>
</style>
<template>
  <div class="editor-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-10 offset-md-1 col-xs-12">
          <form>
            <fieldset>
              <fieldset class="form-group">
                <input
                  v-model="article.title"
                  type="text"
                  class="form-control form-control-lg"
                  placeholder="Article Title"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="article.description"
                  type="text"
                  class="form-control"
                  placeholder="What's this article about?"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  v-model="article.body"
                  class="form-control"
                  rows="8"
                  placeholder="Write your article (in markdown)"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input v-model="tagText" type="text" class="form-control" placeholder="Enter tags" @keyup.enter="onAddTag" />
                <div class="tag-list">
                  <span
                    class="tag-default tag-pill" v-for="(item,index) in article.tagList" :key="index"
                  >
                    <i class="ion-close-round" @click="article.tagList.splice(index,1)"></i>{{item}}
                  </span>
                </div>
              </fieldset>
              <button
                class="btn btn-lg pull-xs-right btn-primary"
                type="button"
                @click.prevent="onCreate"
              >Publish Article</button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { createArticle } from "@/api/article";
export default {
  // nuxt提供的一个特殊的API,在渲染页面的时候会自动调用这个中间件模块中导出的函数
  middleware: ["authenticated"],
  name: "EditPage",
  components: {},
  props: {},
  data() {
    return {
      article: {
        title: "",
        description: "",
        body: "",
        tagList: [],
      },
      tagText:''
    };
  },
  watch: {},
  computed: {},
  methods: {
    async onCreate() {
      const { data } = await createArticle({
        article: this.article,
      });
      console.log(data);
    },
    onAddTag(){
      const tagText = this.tagText
      const {tagList} = this.article
      // 非空校验
      if(!tagText.length){
        return
      }
      // 去重
      if(this.article.tagList.includes(tagText)){
        return
      }
      // 将标签添加到列表中
      tagList.push(tagText)
      // 清空输入框
      this.tagText=''
    }
  },
  created() {},
  mounted() {},
};
</script>

 

相关标签: vue vue