Vue3点击重新加载整个页面

huangapple go评论82阅读模式
英文:

Vue3 click reloading whole page

问题

这是我在Vue3中的代码。后端是Django。

<template>
    <section>
        <div class="container py-5">
            <div class="row">
                <div v-for="item in list" v-bind:key="item.id" class="col-md-8 col-xl-6 text-center mx-auto">
                    <p class="fw-bold text-success mb-2">{{ item.name }}</p>
                    <h3 class="fw-bold">{{ item.age }}</h3>{{ item.vote }}
                    <form @submit.prevent="">
                        <button class="btn" type="button" @click="onClickButtonYes(item.id)">+</button>
                    </form>
                </div>
            </div>
        </div>
    </section>
</template>
<script>
...
async onClickButtonYes(id) {
    const fd = new FormData();
    fd.append("id", id);
    fd.append("isvoted", true);
    axios
        .put(`/api/v1/voteitem/${id}/`, fd)
        .then(response => {
            this.latest = response.data
            this.getlatestitems()
        })
        .catch(error => {
            console.console.log(error);
        })
},
...
</script>

点击按钮后整个页面会重新加载,为什么?

我尝试添加了@submit.prevent="",并添加了type="button"... 问题出在哪里?我向ChatGPT询问过,他提到了响应式。

英文:

this my code in Vue3. Backend is in Django.

&lt;template&gt;
      &lt;section&gt;
        &lt;div class=&quot;container py-5&quot;&gt;
          &lt;div class=&quot;row&quot;&gt;
              &lt;div v-for=&quot;item in list&quot; v-bind:key=&quot;item.id&quot; class=&quot;col-md-8 
                col-xl-6 text-center mx-auto &quot;&gt;
                &lt;p class=&quot;fw-bold text-success mb-2&quot;&gt;{{ item.name }}&lt;/p&gt;
                &lt;h3 class=&quot;fw-bold&quot;&gt;{{ item.age }}&lt;/h3&gt;{{ item.vote }}
                &lt;form @submit.prevent=&quot;&quot;&gt;
                &lt;button class=&quot;btn&quot; type=&quot;button&quot; @click=&quot;onClickButtonYes(item.id)&quot;&gt;
                  +&lt;/button&gt;
                &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
    
      &lt;/section&gt;
    &lt;/template&gt;
    
  
  &lt;script&gt;

...

        async onClickButtonYes(id) {
          const fd = new FormData();
          fd.append(&quot;id&quot;, id);
          fd.append(&quot;isvoted&quot;, true);
          axios
            .put(`/api/v1/voteitem/${id}/`, fd)
            .then(response =&gt; {
              this.latest = response.data
              this.getlatestitems()
            })
            .catch(error =&gt; {
              console.console.log(error);
            })
        },
        
      }
    }
    &lt;/script&gt;

After click button whole page is reloading, why?
I tired add @submit.prevent="", and also add type=button...
Where is problem? I asked chatgpt.. he say something about reactive.

答案1

得分: 1

这是你发布的代码,我不会为你翻译它,但如果你有任何关于这段代码的问题,我可以帮助你解答。

英文:

The problem is outside the code you have posted.

Here is working playground with your code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { createApp, ref } = Vue

const App = {
  setup() {
    const list = ref([{ id: 1, name: &quot;One&quot;, age: 100 }]);
    const onClickButtonYes = (id) =&gt; {
      console.log(id)
       const fd = new FormData();
        fd.append(&quot;id&quot;, id);
        fd.append(&quot;isvoted&quot;, true);
        axios
          .put(`https://nowhere.com/api/v1/voteitem/${id}/`, fd)
          .then(response =&gt; {
            this.latest = response.data
          })
          .catch(error =&gt; {
            console.log(error);
          })
    }
    return {
      list, onClickButtonYes
    }
  }
}

const app = createApp(App)
app.mount(&#39;#app&#39;)

<!-- language: lang-css -->

#app { line-height: 1.75; }
[v-cloak] { display: none; }

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css&quot;  crossorigin=&quot;anonymous&quot;&gt;
&lt;div id=&quot;app&quot; v-cloak&gt;
&lt;section&gt;
    &lt;div class=&quot;container py-5&quot;&gt;
      &lt;div class=&quot;row&quot;&gt;
          &lt;div v-for=&quot;item in list&quot; v-bind:key=&quot;item.id&quot; class=&quot;col-md-8 
            col-xl-6 text-center mx-auto &quot;&gt;
            &lt;p class=&quot;fw-bold text-success mb-2&quot;&gt;{{ item.name }}&lt;/p&gt;
            &lt;h3 class=&quot;fw-bold&quot;&gt;{{ item.age }}&lt;/h3&gt;{{ item.vote }}
            &lt;form&gt;
               &lt;button class=&quot;btn&quot; type=&quot;button&quot; @click=&quot;onClickButtonYes(item.id)&quot;&gt;+&lt;/button&gt;
            &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/section&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 21:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定