如何在脚本中设置if else语句

时间:2019-07-09 13:24:22

标签: laravel if-statement vue.js

-我不会这样做:如果(!editMode)显示创建页面否则显示更新页面,因为我的创建和编辑使用相同的表单,因此我将其合并它。

-我正在学习在线教程,但是该教程显示了分别创建和编辑。

-请帮助并表示感谢/。\

<template>

  <div v-if="!edit">
    <h1>Create Post</h1>
    <form @submit.prevent="addPost">
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label>Post Title:</label>
            <input type="text" class="form-control" v-model="post.title">
          </div>
        </div>
        </div>
        <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              <label>Post Body:</label>
              <textarea class="form-control" v-model="post.body" rows="5"></textarea>
            </div>
          </div>
        </div><br />
        <div class="form-group">
          <button class="btn btn-primary">Create</button>
        </div>
    </form>
  </div>
  <div v-else>
    <h1>Update Post</h1>
    <form @submit.prevent="updatePost">
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label>Post Title:</label>
            <input type="text" class="form-control" v-model="post.title">
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label>Post Body:</label>
            <textarea class="form-control" v-model="post.body" rows="5"></textarea>
          </div>
        </div>
      </div><br />
      <div class="form-group">
        <button class="btn btn-primary">Update</button>
      </div>
    </form>
  </div>
</template>

<script>
export default {
  data(){
    return {
      edit:false,
      post:{}
    }
  },
},
//this is for create
methods: {
  addPost(){
    let uri = 'http://localhost:8000/post/create';
    this.axios.post(uri, this.post).then((response) => {
      this.$router.push({name: 'posts'});
    });
  },
},
//this is for get data before update
created() {
  let uri = `http://localhost:8000/post/edit/${this.$route.params.id}`;
  this.axios.get(uri).then((response) => {
    this.post = response.data;
  });
},
//this is for update post
updatePost() {
  let uri = `http://localhost:8000/post/update/${this.$route.params.id}`;
  this.axios.post(uri, this.post).then((response) => {
    this.$router.push({name: 'posts'});
  });
}
</script>


**I also set this in my app.js**  
{
      name: 'create',
      path: '/create',
      component: PostForm,
      props: {editMode: false}
  },
  {
      name: 'edit',
      path: '/edit/:id',
      component: PostForm,
      props: {editMode: true}
  }

我的错误--->当我按下编辑btn显示创建页面并使用addPost函数时。

结果->如何使用其他方式解决此问题。...对不起,我是编程新手。

1 个答案:

答案 0 :(得分:2)

我相信您可以简单地让js找出添加或更新的内容。

<template>
  <div>
    <h1 v-if="!edit">Create Post</h1>
    <h1 v-else>Update Post</h1>
    <form @submit.prevent="postSomething">
        <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              <label>Post Title:</label>
              <input type="text" class="form-control" v-model="post.title">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              <label>Post Body:</label>
              <textarea class="form-control" v-model="post.body" rows="5"></textarea>
            </div>
          </div>
        </div><br />
        <div class="form-group">
          <button v-if="!edit" class="btn btn-primary">Create</button>
          <button v-else class="btn btn-primary">Update</button>
        </div>
        </form>
    </div>

</template>

和js:

methods: {
    postSomething(){
      if(!this.edit){
        this.addPost()
      }else{
        this.updatePost()
      }
    },
    addPost(){
      console.log('should add')
    },
    updatePost(){
      console.log('should update')
    }
  }