【发布时间】:2020-09-10 04:52:35
【问题描述】:
同时我遇到另一个错误,即“事件“单击”的处理程序无效”。
<template>
<div id="example-2">
<!-- `greet` is the name of a method defined below -->
<button v-on:click="greet">Greet</button>
</div>
</template>
<script>
window.onload = function () {
var example2 = new Vue({
el: '#example-2',
data: {
name: 'Vue.js'
},
// creating method greet
methods: {
greet: function (event) {
// `this` inside methods points to the Vue instance
alert('Hello ' + this.name + '!')
// `event` is the native DOM event
if (event) {
alert(event.target.tagName)
}
}
}
})
}
</script>
【问题讨论】:
-
从未见过有人将脚本内容放在
window.onload = function () {内的 .vue 文件中...尽量不要这样做 -
这是什么文件?除非您正在编写单文件 Vue 组件,否则您不能真正使用
<template>标签。如果它是单文件组件,您通常不会在其中创建新的Vue实例。见vuejs.org/v2/guide/single-file-components.html -
@JaromandaX 如果我删除“window.onload = function ()”,我会收到此错误。 “找不到元素:#example-2”
-
@Phil 那么你能指导我在没有 的情况下执行这个新的 vue 实例吗?
标签: javascript html vue.js