<body><divid="app"><!--3.my-component只能在#app下使用--><my-component></my-component></div><script>//1.创建一个组件构造器var myComponent=Vue.extend({
template:'<div>this is my first component!</div>'})var vm=newVue({
template:'<div>this is my first component!</div>'})var vm=newVue({
el:'#app',
components:{
//2.将myComponent组件注册到vue实例下'my-component':myComponent
}});</script></body>
2. 验证组件全局注册的用法
<body><divid="app"><!--3.#app是vue实例挂载的元素,应该在挂载元素范围内使用组件--><my-component></my-component></div><divid="app2"><my-component></my-component></div><my-component></my-component><script>//1.创建一个组件构造器var myComponent=Vue.extend({
template:'<div>This is my first component!</div>'})//2.注册组件,并指定组件的标签,组件的HTML标签为<my-component>
Vue.component('my-component',myComponent)var vm=newVue({
el:'#app'});var vm1=newVue({
el:'#app2'})</script></body>
3. 验证通过组件注册语法糖简化全局注册步骤
<body><divid="app1"><my-component1></my-component1></div><divid="app2"><my-component2></my-component2><my-component3></my-component3><my-component1></my-component1></div><script>//全局注册,my-component1是标签名称
Vue.component('my-component1',{
template:'<div>this is the first component!</div>'})var vm1=newVue({
el:'#app1'})var vm2=newVue({
el:'#app2',
components:{
//局部注册,my-component2是标签名称'my-component2':{
template:'<div>this is the second component!</div>'},//局部注册,my-component3是标签名称'my-component3':{
template:'<div>this is the third component!</div>'}}})</script></body>