<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"></head><body><script type="text/javascript" src="./vue.js"> </script><div id="app" ><!-- 编写组件标签 --><My> </My></div><script type="text/javascript">// 创建一个student组件const Student = Vue.extend({template:`<div><h3>学生名称: {{ studentName}} </h3><h3>学生年龄: {{ age }} </h3><button @click="addage">年龄增加</button></div>`,data(){return {studentName: '张三',age:18,}},methods:{addage(){this.age ++}}})//注意这里,注册组件一定要在vm前面Vue.component("My",Student);var vm = new Vue({el:"#app",data:{myage: 18,},})</script></body></html>
注意 注册组件这个步骤一定要在vm前面
