vue输入框添加\删除标签

<!--
 * @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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值