1 Star 0 Fork 0

liucming / vueExample

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
父子组件之间传值.html 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
LuckyGirlMing 提交于 2020-09-22 16:57 . vue例子代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<com1 v-bind:parentmsg="msg" @func="getMsgFormSon"></com1>
</div>
<template id="tmpl">
<div>
<h1>这是子元素 --- {{ parentmsg }}</h1>
<input type="button" value="向父组件传递消息" @click="sendMsg">
</div>
</template>
<script>
var com1 = {
template: '#tmpl',
data() {
return {
msg: '做一个孝顺的孩子,给爸爸一些钱去挥霍吧!'
}
},
props: ['parentmsg'],
methods: {
sendMsg() {
// 调用父组件传递过来的方法,同时把数据传递出去
this.$emit('func', this.msg)
}
}
}
// 创建 Vue 实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {
msg: '这是父组件中的数据,爸爸有100块钱,my son, 你要不',
msgFormSon: ''
},
methods: {
getMsgFormSon(data) {
this.msgFormSon = data
console.log(this.msgFormSon)
}
},
components: {
com1
}
});
</script>
</body>
</html>
JavaScript
1
https://gitee.com/liucming/vue-example.git
git@gitee.com:liucming/vue-example.git
liucming
vue-example
vueExample
master

搜索帮助