组件
前端组件化发展历史
前后端耦合
前后端不分离项目
找后台搭建项目开发环境 寻找项目目录中的静态资源目录 同步修改css 前后端分离 前端团队合作项目
使用组件的优势
组件的概念
组件是一个 html、css、js、img 等的一个聚合体 组件中的 options 大体上和Vue中的 options 是一致的
即 data / methods / watch / computed 等在组件中均可使用 Vue中的组件属于扩展性功能,通过 Vue.extend() 来扩展的 组件是对html标签的扩展【 后端发展来的,借鉴了 angular / react 】
ƒ Vue ( options) {
if ( ! ( this instanceof Vue )
) {
warn ( 'Vue is a constructor and should be called with the `new` keyword' ) ;
}
this . _init ( options) ;
}
ƒ VueComponent ( options) {
this . _init ( options) ;
}
组件的注册
组件是以标签的形式使用的,但其不符合html标准,故需要解析,这个过程叫组件的注册 <Banner></Banner> -> <div></div>
组件要想合法化,必须注册解析
- 全局注册
< body>
< div id = " app" >
< Hello> </ Hello>
< head-title> </ head-title>
< Hello/>
</ div>
< div id = " root" >
< Hello> </ Hello>
</ div>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
var Hello = Vue. extend ( {
template: '<div> hello 组件 </div>' ,
} )
Vue. component ( 'Hello' , Hello )
Vue. component ( 'HeadTitle' , Hello)
new Vue ( {
el: '#app'
} )
new Vue ( {
el: '#root'
} )
</ script>
- 局部注册
< body>
< div id = " app" >
< Hello> </ Hello>
</ div>
< div id = " root" >
< Hello/>
</ div>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
var Hello = Vue. extend ( {
template: '<div> Hello 组件 </div>'
} )
new Vue ( {
el: '#app' ,
components: {
'Hello' : Hello
}
} )
new Vue ( {
el: '#root'
} )
</ script>
- 组件的简写
< script>
Vue. component ( 'Hello' , {
template: '<div> hello 组件 </div>' ,
} )
new Vue ( {
el: '#app'
} )
</ script>
----------------------------------------
< script>
new Vue ( {
el: '#app' ,
components: {
'Hello' : {
template: '<div> Hello 组件 </div>'
}
}
} )
</ script>
< body>
< div id = " app" >
< Hello> </ Hello>
< template>
< p> 456 </ p>
</ template>
</ div>
< Hello> </ Hello>
<template id = "hello">
< div>
Hello 组件
< p> 123 </ p>
</ div>
</ template>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
Vue. component ( 'Hello' , {
template: '#hello'
} )
new Vue ( {
el: '#app'
} )
</ script>
< body>
< div id = " app" >
< table>
< tr>
< td> 1</ td>
< td> 2</ td>
< td> 3</ td>
</ tr>
<tr is = "Hello"></ tr>
</ table>
</ div>
<template id = "hello">
< tr>
< td> 4</ td>
< td> 5</ td>
< td> 6</ td>
</ tr>
</ template>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
Vue. component ( 'Hello' , {
template: '#hello'
} )
new Vue ( {
el: '#app'
} )
</ script>
< body>
< div id = " app" >
< Hello> </ Hello>
</ div>
<template id = "hello">
< div>
< h3> hello </ h3>
<input type = "text" v-model = "msg">
< p> {{ msg }} </ p>
<button @click = "handler"> 点击 </ button>
< p>
{{ newMsg }}
</ p>
< p> {{ title }} </ p>
</ div>
</ template>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
Vue. component ( 'Hello' , {
template: '#hello' ,
data ( ) {
return {
msg: 'Vue.js' ,
title: ''
}
} ,
watch: {
msg ( ) {
this . title = this . msg
}
} ,
methods: {
handler ( ) {
alert ( '方法' )
}
} ,
computed: {
newMsg: {
get ( ) {
return this . msg. split ( '' ) . reverse ( ) . join ( '' )
}
}
}
} )
new Vue ( {
el: '#app'
} )
</ script>
- 全局嵌套
< body>
< div id = " app" >
< Father> </ Father>
</ div>
< template id = " father" >
< div>
< h3> father </ h3>
< hr>
< Son/>
</ div>
</ template>
< template id = " son" >
< h5> son </ h5>
</ template>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
Vue. component ( 'Father' , {
template: '#father'
} )
Vue. component ( 'Son' , {
template: '#son'
} )
new Vue ( {
el: '#app'
} )
</ script>
- 局部嵌套
< script>
new Vue ( {
el: '#app' ,
components: {
'Father' : {
template: '#father' ,
components: {
'Son' : {
template: '#son'
}
}
}
}
} )
</ script>
< body>
< div id = " app" >
<Hello :name = "name" @click.native = "changeName"></ Hello>
</ div>
< template id = " hello" >
<div style = "width: 100px;height: 100px;background: red;">
< p> {{ name }} </ p>
</ div>
</ template>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
Vue. component ( 'Hello' , {
template: '#hello' ,
props: [ 'name' ]
} )
new Vue ( {
el: '#app' ,
data: {
name: 'Vue 1.0'
} ,
methods: {
changeName ( ) {
this . name = "Vue 2.0"
}
}
} )
</ script>
< body>
< div id = " app" >
<select name="" id="" v-model = "val">
< option value = " father" > father </ option>
< option value = " son" > son </ option>
</ select>
<keep-alive :include = "val">
<component :is = "val"></ component>
</ keep-alive>
</ div>
</ body>
< script src = " ../../../lib/vue.js" > </ script>
< script>
Vue. component ( 'Father' , {
template: '<div> father </div>' ,
activated ( ) {
console. log ( 'father-activated' )
} ,
} )
Vue. component ( 'Son' , {
template: '<div> son </div>' ,
activated ( ) {
console. log ( 'son-activated' )
} ,
} )
new Vue ( {
el: '#app' ,
data: {
val: 'Father'
} ,
activated ( ) {
console. log ( 'inactived' )
} ,
deactivated ( ) {
console. log ( "deactivated" )
}
} )
</ script>