在执行POST / PUT请求之前,如何检查字符串是否存在?

红移

我正在使用VueJS,并且得到了一个Web表单,用户可以在其中检查标记为“活动”的html复选框,以指定项目(名为branch)是否为active如果未选中输入框,则表示该项目为inactive如果abranchinactive,则将文本动态附加(inactive)branch属性中。然后,将来自表单的数据收集发送到数据库/远程api。非常简单。以下UI示例:

在此处输入图片说明 在此处输入图片说明

我的问题是:如果分支名称已存在,如何删除该字符串(inactive)

这是我的data()对象,并且相关method()

  data() {
    return {
      branch: {
        division_id: null,
        branch: '',
        active: false,
        branch_id: null
      },

onSubmitUpdate() {
      this.loading = true
      let branchEdit = this.branch
      branchEdit.branch = this.branch.active ? this.branch.branch : this.branch.branch + ' (inactive)'
      ApiService.updateBranch(branchEdit)
        .then(() => {
          this.loading = false
          //this.$router.push({ path: '/home' })
        })

感谢您提供有关如何有效执行此操作的提示!

尼基尔

您可以使用String.prototype.replace()替换字符串(如果存在)。

branchEdit.branch = this.branch.active ? 
    this.branch.branch.replace(' (inactive)', '') : 
    this.branch.branch + ' (inactive)';

如果分支名称包含字符串(inactive),则将其删除。如果没有,则什么都不会改变。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章