<template>
<div>
<h1 v-on:click="handleClick">Test</h1>
</div>
</template>
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
@Component
export default class TestComponent extends Vue {
created() {
console.log("Initialized vue component");
}
public handleClick() {
console.log("Clicked element");
}
}
</script>
主页DOM中元素的标记,应将Vue组件加载到其中:
<div id="test"></div>
import Vue from "vue";
import TestComponent from "./TestComponent";
const test = new Vue({
el: '#test',
components: {
'test-component': TestComponent
}
});
控制台输出:
加载后:“初始化的vue组件” /点击时:没有任何内容
window.addEventListener("load", function(event) {
const test = new Vue({
el: '#test',
components: {
'test-component': TestComponent
}
});
});
加载后:“初始化的vue组件” /点击时:“被点击的元素”
document.addEventListener("DOMContentLoaded", function () {
const test = new Vue({
el: '#test',
components: {
'test-component': TestComponent
}
});
});
加载后:“初始化的vue组件” /点击时:“被点击的元素”
const test = new Vue({
components: {
TestComponent
},
render: (h) => h(TestComponent),
}).$mount('#test');
加载后:“初始化的vue组件” /点击时:“被点击的元素”
const test = new Vue({
components: {
TestComponent
},
render: (h) => h(TestComponent),
});
document.addEventListener("DOMContentLoaded", function () {
test.$mount('#test');
});
加载后:“初始化的vue组件” /点击时:“被点击的元素”