获取DOM元素
<body>
<div id="box">
<h1 ref='h' id="myh">1111h1>
<button id="btn" @click="show($event)">按钮button>
div>
body>
<script>
new Vue({
el: '#box',
data: {
msg: 'hello'
},
methods: {
show(e) {
var v = this.$refs.h;
v.innerText = "bbbbbbbbbbbbbbbbbbbbbb";
}
}
})
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
定义全局组件
<body>
<template id="top">
<div style="height:100px;background:red;margin-top:10px">
<h1>我的顶部组件-{{text}}h1>
<button @click="show()">一个按钮{{count}}button>
<button>一个按钮button>
div>
template>
<div id="box">
<my-top>my-top>
div>
body>
<script type="text/javascript">
Vue.component('my-top', {
template: "#top",
data: function() {
return {
text: '我是组件的数据',
count: 0
}
},
methods: {
show() {
this.count++;
}
},
mounted: function() {
}
});
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
methods: {}
})
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
定义局部组件
<body>
<template id="left">
<div style="height:300px;width:200px;background:greenyellow;margin-top:10px">
<h1>我的左边的组件-{{ text }}h1>
<button @click="show()">一个按钮{{ count }}button>
div>
template>
<div id="box">
<my-left>my-left>
div>
body>
<script type="text/javascript">
var myLeft = {
template: '#left',
data: function() {
return {
text: '我是组件的数据',
count: 0
}
},
methods: {
show() {
this.count++;
}
}
}
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
components: {
'my-left': myLeft
}
})
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
<body>
<template id="top">
<div style="height:100px;background:red;margin-top:10px">
<h1>我的顶部组件-{{text}}h1>
<button @click="show()">一个按钮{{count}}button>
<son-son>son-son>
div>
template>
<div id="box">
<my-top>my-top>
div>
body>
<script type="text/javascript">
var myTop = {
template: "#top",
data: function () {
return {
text: '我是组件的数据',
count: 0
}
},
methods: {
show() {
this.count++;
}
},
components: {
'son-son': {
template: `
子子组件
`,
data: function () {
return {
text: '我是组件的数据',
count: 0
}
},
methods: {
show() {
this.count++;
}
}
}
}
}
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
components: {
'my-top': myTop
}
})
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
父组件给子组件传递数据
<body>
<template id="son">
<div>
<h1>
子组件{{sonmsg}}---{{hehe}}---{{haha}}
h1>
<h2>{{num}}h2>
<h2>{{arr[0]}}h2>
<h2>{{flag}}h2>
<h2>{{obj.username}}h2>
div>
template>
<div id="box">
<my-son :haha="msg" :num="num" :arr="arr" :flag="flag" :obj="obj">my-son>
div>
body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
msg: 'Hello',
num: 100,
arr: [20, 50, 80],
flag: true,
obj: {
"username": "张三"
}
},
components: {
'my-son': {
template: '#son',
props: ['hehe', 'haha', 'num', 'arr', 'flag', 'obj'],
data: function() {
return {
sonmsg: '子组件的数据'
}
}
}
}
})
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
子组件给父组件传递数据
<body>
<template id="son">
<div>
<h1>我是子组件{{sonmsg}}h1>
<button @click="sendData()">把子组件的数据传递给父组件button>
div>
template>
<div id="box">
{{msg}}
<son @send-data="jieShouData" @send-username="jieShouName" @send-object="jieShouObj">son>
<son @send-data="jieShouData($event)" @send-username="jieShouName($event)"
@send-object="jieShouObj($event)">son>
div>
body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
msg: 'Hello'
},
methods: {
jieShouData(value) {
alert("父组件收到:" + value);
},
jieShouName(v) {
alert("父组件收到:" + v);
},
jieShouObj(obj) {
alert(obj.username + "===" + obj.num);
}
},
components: {
'son': {
template: '#son',
data: function() {
return {
sonmsg: '我是子组件的数据',
num: 100,
username: '王五'
}
},
methods: {
sendData() {
this.$emit('send-data', this.num)
this.$emit('send-username', this.username);
var json = {
"num": this.num,
"username": this.username
}
this.$emit('send-object', json);
}
}
}
}
})
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67