<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript" src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app"> </div> <script> //1.创建一个组件 var MyCompose = { //组件中不能有el el: "#app", //data 变了,data需要为函数 data() { return{ firstname: "dai", secondname: "liang", } }, //method和computed都可以保留 methods: { fullname() { return this.firstname + this.secondname } }, template: ` <h3> {{ firstname }} {{ secondname }} {{ fullname() }} </h3> ` }; //2.全局注册组件,注意最好使用驼峰写法 // Vue.component("MyComp",MyCompose); //3.使用组件 var vm = new Vue({ el:"#app", //4.局部注册组件 components: { MyCompose, }, template:` <MyCompose></MyCompose> `, }) </script> </body></html>