全局样式
<div id='app'>
<my-component></my-component>
<ul>
<li></li>
</ul>
</div>
....
//全局:
Vue.component('my-component',{
template:`
<ul>
<li></li>
</ul>
`
})
new Vue({
el:'#app',
components:{
'my-component':{
template:``
}
}
})
局部
<div class:'nr'>
<my-component><.my-conmponent>
</div>
<script scr:"js链接"></script>
<script>
new Vue({
el:".nr",
components:{
"my-component":{
template:`
<li>这是组件部分</li>
`
}
}
})
进阶数字
<div id='app'>
<!-- <p>{{msg}}</p>-->
<my-component></my-component>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component("my-component",{
template:`
<div>
<h1>{{msg}}</h1>
<button @click='alt'>按钮</button>
</div>
`,
data:function(){
return{
msg:'dcgf'
}
},
methods:{
alt:function(){
alert(11111)
}
}
})
new Vue({
el:'#app',
})
</script>
元素属性
父传子
<div id='app'>
<my-content></my-content>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component("my-content",{
template:`
<div>
<h2>我是my-content组件的标题</h2>
<my-child v-bind:message='msg'></my-child>
</div>
`,
data:function(){
return{
msg:'dgddbghfghfnh'
}
}
})
Vue.component("my-child",{
props:['message'],
template:`
<div>
<h3>我是my-child组件中的标题</h3>
<p>{{message}}</p>
</div>
`
})
new Vue({
el:'#app'
})
</script>
子传父
<div id='app'>
<my-father></my-father>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component("my-father",{
template:`
<div>
<my-son v-bind:tit='title'></my-son>
<my-list v-bind:fruit='arr'></my-list>
</div>
`,
data:function(){
return{
arr:['apple','pear','orange'],
title:'水果列表'
}
}
})
//title
Vue.component('my-son',{
props:['tit'],
template:`
<h2>{{tit}}</h2>
`
})
//arr
Vue.component('my-list',{
props:['fruit'],
template:`
<ul>
<li v-for="value in fruit">{{value}}</li>
</ul>
`
})
new Vue({
el:'#app'
})
</script>