书签 分享 收藏 举报 版权申诉 / 148

类型vue文档重点学习.docx

  • 文档编号:27753261
  • 上传时间:2023-07-04
  • 格式:DOCX
  • 页数:148
  • 大小:224.62KB

//1.创建一个组件构造器

varcomponent1=Vue.extend({

template:

'

helloworld
'

});

//2.注册组件,并指定组件的标签为

Vponent('component1',component1);

//3.实例化组件

newVue({

el:

'#container'

});

浏览器编译后html结构会变为 

helloworld

页面运行显示为 

helloworld

2.理解组件的创建和注册。

2-1Vue.extend()是Vue构造器的扩展,调用Vue.extend()创建的是一个组件构造器,该构造器有一个选项对象,选项对象的template属性用于定义组件要渲染的html。

2-2Vponent()是注册组件,需要2个参数,第一个参数是自定义组件的标签,第二个参数是组件的构造器。

2-3组件需要挂载到某个Vue的实例下,否则不生效。

如下实例:

DOCTYPEhtml>

演示Vue

//1.创建一个组件构造器

varcomponent1=Vue.extend({

template:

'

helloworld
'

});

//2.注册组件,并指定组件的标签为

Vponent('component1',component1);

//3.实例化组件container1

newVue({

el:

'#container1'

});

//3.实例化组件container2

newVue({

el:

'#container2'

});

//不实例化container3因此第三个自定义标签是不会生效的

最终代码被渲染成为如下:

helloworld
helloworld

3.理解Vue全局注册和局部注册

调用Vponent()注册组件时,组件的注册是全局的,如果想要使用组件的局部注册的话,可以用选项对象的components属性实现局部注册。

如下代码:

中间就把第二步注册组件哪项移到实例化组件里面来了;如下代码:

DOCTYPEhtml>

演示Vue

--不能使用component1组件,因为它是container1里面局部注册的-->

//1.创建一个组件构造器

varcomponent1=Vue.extend({

template:

'

helloworld
'

});

//3.实例化组件container1

newVue({

el:

'#container1',

components:

{

'component1':

component1

}

});

//实例化container2是不生效的

newVue({

el:

'#container2'

})

实例化container2是不生效的,并且在浏览器控制台会报如下错误:

4.理解父组件和子组件。

在一个组件中包含另一个组件,那么另一个组件就是该组件的子组件。

如下代码:

DOCTYPEhtml>

演示Vue

//1.创建一个组件构造器

varChild=Vue.extend({

template:

'

helloworld
'

});

varParent=Vue.extend({

//在组件内部使用组件

template:

'

helloworld
',

components:

{

//局部注册Child组件

'child-component':

Child

}

});

//全局注册Parent组件

Vponent('parent-component',Parent);

//实例化组件

newVue({

el:

'#container1'

})

简单理解代码如下:

1.varChild=Vue.extend(...)定义一个Child组件构造器。

2.varParent=Vue.extend(...)定义一个Parent组件构造器。

3.components:

{'child-component':

Child},将Child组件注册到Parent组件,并将Child组件的标签设置为child-component;

4.template:

渲染html模板,找到template选项,然后使用child-component组件。

5.注册Parent组件Vponent('parent-component',Parent);

6.最后实例化组件,需要到html元素为id='container1'里面去。

Child组件是在Parent组件中注册的,只能在Parent组件中注册的。

如下几种情况都不行的。

4-1以子标签的形式在父组件中使用;如下代码:

DOCTYPEhtml>

演示Vue

//1.创建一个组件构造器

varChild=Vue.extend({

template:

'

helloworld
'

});

varParent=Vue.extend({

//在组件内部使用组件

template:

'

helloworld
',

components:

{

//局部注册Child组件

'child-component':

Child

}

});

//全局注册Parent组件

Vponent('parent-component',Parent);

//实例化组件

newVue({

el:

'#container1'

})

上面调用子组件的方式是无效的,因为在js里面当父组件要需要的html模板template的内容的时候已经决定了需要渲染什么,所以当parent-component运行的时候,在父组件使用自定义的子标签。

运行时会当做html的普通标签来渲染,但是它又不是普通的html标签,因此会被忽略掉。

4-2.在父组件标签外使用子组件。

js代码还是上面的一样,运行完成后,在浏览器下会报错如下:

5.理解组件的语法糖。

我们可以使用更简单的方式来注册组件。

5-1使用Vponent()直接创建和注册组件。

如下代码:

DOCTYPEhtml>

演示Vue

//全局注册

Vponent('component1',{

template:

'

helloworld222
'

});

//实例化

varvm1=newVue({

el:

'#container1'

});

Vponent()的第一个参数是标签名称,第二个参数是一个选项对象,使用选项对象的template属性定义,使用该方式,在Vue源码中会调用Vue.extend()方法。

注意:

在template元素中需要使用一个标签容器包围,比如我们可以把div元素去掉的话,只放内容的话,会报错如下:

5-2在选项对象的components属性中实现局部注册。

DOCTYPEhtml>

演示Vue

//全局注册,my-component1是标签名称

Vponent('component1',{

template:

'

Thisisthefirstcomponent!

'

})

varvm1=newVue({

el:

'#container1'

})

//实例化局部注册

varvm1=newVue({

el:

'#container2',

components:

{

//局部注册,component2是标签名称

'component2':

{

template:

'

component2
'

},

//局部注册,component3是标签名称

'component3':

{

template:

'

component3
'

}

}

});

6.学会使用script或template标签。

虽然语法糖简化了组件注册,但是在template选项中拼接了html元素,这导致View和C层的高耦合性。

幸运的是Vue.js提供了2种方式将javascript中的html模板分离出来。

6-1使用script标签, 如下代码:

DOCTYPEhtml>

演示Vue

helloworld

newVue({

el:

'#container1',

components:

{

'component1':

{

template:

'#myComponent'

}

}

})

注意:

使用

newVue({

el:

'#container1',

components:

{

'component1':

{

template:

'#myComponent'

}

}

})

7.理解使用props。

父组件的数据如何传给子组件呢?

可以使用props把数据传给子组件。

代码如下:

DOCTYPEhtml>

演示Vue

my-name="name"v-bind:

my-age="age">

子组件数据

myName{{myName}}
myAge{{myAge}}

newVue({

el:

'#container1',

data:

{

name:

'longen',

age:

30

},

components:

{

'component1':

{

template:

'#myComponent',

props:

['myName','myAge']

}

}

})

注意:

在子组件中定义prop时,使用了camelCase命名法。

由于HTML特性不区分大小写,camelCase的prop用于特性时,会转为短横线隔开的,比如上面的代码:

在props中定义的myName,在用作特性时需要转换为my-name

理解prop的单向绑定

既然父组件使用props把数据传给了子组件,那么如果子组件修改了数据,对父组件是否有影响呢?

看下面的代码如下:

DOCTYPEhtml>

演示Vue

父组件数据

name{{name}}
age{{age}}

my-name="name"v-bind:

my-age="age">

子组件数据

myName{{myName}}
myAge{{myAge}}

举报
举报
版权申诉
版权申诉
word格式文档无特别注明外均可编辑修改;预览文档经过压缩,下载后原文更清晰! 立即下载
配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
vue 文档 重点 学习
提示  冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:vue文档重点学习.docx
链接地址:https://www.bdocx.com/doc/27753261.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2022 冰点文档网站版权所有

经营许可证编号:鄂ICP备2022015515号-1

收起
展开