1.全局组件必须写在Vue实例创建之前,才在该根元素下面生效;
正确写法
Vue.component("my-component",{ template:"<h1>我是全局组件</h1>" }); new Vue({ el:"#app" }); new Vue({ el:"#app1" }) </script>
错误写法
<div id="app"> <my-component></my-component> </div> <div id="app1"> <my-component></my-component> </div> <script> new Vue({ el: "#app" }); Vue.component("my-component", { template: "<h1>我是全局组件</h1>" }); new Vue({ el: "#app1" }) </script>
调用组件,就像是使用html标签一样