vue 子元件向父元件傳遞資訊
1.要引入vue,js
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
<parent>
</parent>
</div>
<template id="parent">
<div>
<p>父元件</p>
<h1 v-text="name"></h1>
<p>{{"這個是子元件的資料:"+gg}}</p>
<chlid @zdy="getdata"></chlid>
</div>
</template>
<template id="chlid">
<div>
<p>子元件</p>
<h2 v-text="na"></h2>
<button @click="send">子元件向父元件傳遞資訊</button>
</div>
</template>
<script src="../vue.js"></script>
<script type="text/javascript">
new Vue({
el: "#app",
data: {
},
components: {
"parent": {
data() {
return {
name: "這個是父元件",
str: "簡介",
gg: ""
}
},
methods: {
getdata(data) {
this.gg = data;
}
},
template: '#parent',
components: {
"chlid": {
props: ["a", "b"],
data() {
return {
na: "這個是子元件",
name: "123456"
}
},
template: '#chlid',
methods: {
send() {
this.$emit("zdy", this.name)
}
}
}
}
}
}
})
</script>
</body>
</html>