代码之家  ›  专栏  ›  技术社区  ›  MathieuAuclair

为什么Vue.js模板不能与它的脚本交互?

  •  0
  • MathieuAuclair  · 技术社区  · 8 年前

    我使用VAU.JS和AXIOS,当我点击某个按钮时,我试图做一个Ajax调用。我的问题是按钮的onclick事件找不到它的函数。有人能给我解释一下吗 为什么我的模板不能在我的视图中与我的脚本交互?

    示例.vue

    <template>
        <button onclick="someClickFunction();"></button>
    </template>
    
    <script>
        function someClickFunction(){
            console.log("You've pressed the button");
        }
    
        export default {
            name: "button-view",
        }
    </script>
    
    <style scoped>
    </style>
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   Nicolas    8 年前

    在vue中,必须在methods对象中编写函数,onclick==@click

    <template>
            <button @click="someClickFunction();"></button>
        </template>
    
        <script>
            export default {
                name: "button-view",
                methods: {
                  someClickFunction(){
                    console.log("You've pressed the button");
                  }
                }
            }
        </script>
    
        <style scoped>
        </style>
    
        2
  •  1
  •   keysl    8 年前

    这是因为在vue中,不使用本地onclick事件。

    相反,您使用

    v-on:click
    

    或者

    @click
    

    https://vuejs.org/v2/guide/events.html

    编辑: