一、组件间通信(父组件    -->  子组件)
步骤:
①父组件在调用子组件 传值
<child-component myValue="123"> </child-component>
②在子组件中 获取父组件传来的值
Vue.component('child-component',{
  props:['myValue'],
  template:''
})

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>父传子</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<parent-component></parent-component>
</div>
<script>
// 在vue中一切都是组件
//父传子
Vue.component("parent-component",{
data:function(){
return {
gift:"传家宝"
}
},
template:`
<div>
<h1>这是父组件</h1>
<hr>
<child-component v-bind:myValue="gift"></child-component>
</div>
`
})
Vue.component("child-component",{
props:["myValue"],
template:`
<div>
<h1>这是子组件</h1>
<p>{{"父传递的值:"+myValue}}</p>
</div>
`
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
myValue是属性名,必须都一样……拿data中的用v-bind:或者:
props是property属性,是个数组
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>父子之间通信练习</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<my-login></my-login>
</div>
<script>
/*
登录窗口
创建4个组件,分别是my-label my-input my-button my-login(复合组件)
*/
Vue.component("my-label",{
props:["myLabel"],
template:`
<div>
<label>{{myLabel}}</label>
</div>
`
})
Vue.component("my-input",{
template:`
<div>
<input type="text"/>
</div>
`
})
Vue.component("my-button",{
props:["myButton"],
template:`
<div>
<button>{{myButton}}</button>
</div>
`
})
//复合组件
Vue.component("my-login",{
data:function(){
return {
uname:"用户名",
upwd:"密码",
login:"登录",
register:"注册"
}
},
template:`
<div>
<my-label v-bind:myLabel="uname"></my-label>
<my-input></my-input>
<my-label v-bind:myLabel="upwd"></my-label>
<my-input></my-input>
<my-button v-bind:myButton="login"></my-button>
<my-button v-bind:myButton="register"></my-button>
</div>
`
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="js/vue.js"></script>
<title></title>
</head>
<body> <div id="container">
<my-login></my-login>
</div> <script> Vue.component('my-label',{
props:['labelName'],
template:'<label>{{labelName}}</label>'
})
Vue.component('my-input',{
props:['tips'],
template:'<input type="text" :placeholder="tips"/>'
})
Vue.component('my-button',{
props:['btnName'],
template:'<button>{{btnName}}</button>'
}) Vue.component('my-login',{
template:`
<form>
<my-label labelName="用户名"></my-label>
<my-input tips="请输入用户名"></my-input>
<br/>
<my-label labelName="密码"></my-label>
<my-input tips="请输入密码"></my-input>
<br/>
<my-button btnName="登录"></my-button>
<my-button btnName="注册"></my-button>
</form>
`
}) new Vue({
el: '#container',
data: {
msg: 'Hello Vue'
}
})
</script> </body>
</html>

要拿到data中的数据就要v-bind,否则就不用

vue组件父与子通信-登录窗口的更多相关文章

  1. vue 组件-父组件传值给子组件

    父组件通过属性,传值给子组件,子组件通过,props数组里的名称来接受父组件传过来的值. HTML部分: <div id="app"> <tmp1 :parent ...

  2. 深入理解Vue组件3大核心概念

    摘要: 搞懂Vue组件! 作者:浪里行舟 原文:详解vue组件三大核心概念 Fundebug经授权转载,版权归原作者所有. 前言 本文主要介绍属性.事件和插槽这三个vue基础概念.使用方法及其容易被忽 ...

  3. vue 组件之间相互传值 父传子 子传父

    1.父传子 把要传入的值放到父标签里  子组件使用props接收 父写法 子写法 2.子传父 子组件: childrenOnclick() { // 发布自定义事件 this.$emit(" ...

  4. 【转】Vue组件一-父组件传值给子组件

    Vue组件一-父组件传值给子组件 开始 Vue组件是学习Vue框架最比较难的部分,而这部分难点我认为可以分为三个部分学习,即 组件的传值 - 父组件向子组件中传值 事件回馈 - 子组件向父组件发送消息 ...

  5. Vue.js父与子组件之间传参

    父向子组件传参 例子:App.vue为父,引入componetA组件之后,则可以在template中使用标签(注意驼峰写法要改成componet-a写法,因为html对大小写不敏感,component ...

  6. Vue把父组件的方法传递给子组件调用(评论列表例子)

    Vue把父组件的方法传递给子组件调用(评论列表例子) 效果展示: 相关Html: <!DOCTYPE html> <html lang="en"> < ...

  7. Vue中父组件向子组件传值

    Vue中父组件向子组件传值 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  8. vue 实现父组件和子组件之间的数据双向绑定

    前言:vue 实现父组件给子组件传值,然后子组件可以修改回父组件的值.vue 的 prop 默认是单向数据绑定,但是偶尔需要双向绑定,这时就需要知道如何才能让子组件的数据修改时影响到父组件的数据.转载 ...

  9. 【vue】父向子组件传参、子组件向父传参

    1.父向子组件传参 App.vue为父,引入componetA组件之后,则可以在App.vue中使用标签(注意驼峰写法要改成componet-a写法,因为html对大小写不敏感,componenta与 ...

随机推荐

  1. react随笔

    对React children 的深入理解     https://www.jianshu.com/p/d1975493b5ea [react]利用prop-types第三方库对组件的props中的变 ...

  2. Mybatis驼峰式命名

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  3. Qt项目中main主函数及其作用

    http://c.biancheng.net/view/1821.html main.cpp 是实现 main() 函数的文件,下面是 main.cpp 文件的内容. #include "w ...

  4. Linux下安装chrome浏览器

    第一步:进入google-chrome官网下载chrome安装包 官网地址:https://www.google.cn/chrome/ 选择要下载的安装包 注意:这里有两个选项,请按照你安装的系统下载 ...

  5. [转载]转一篇Systemverilog的一个牛人总结

    原文地址:转一篇Systemverilog的一个牛人总结作者:dreamylife Systemverilog 数据类型 l       合并数组和非合并数组 1)合并数组: 存储方式是连续的,中间没 ...

  6. 企业微信的corpsecret在哪里?

      问题: 查看“企业微信”的官方开发文档,在“获取access_token”部分提到,使用GET请求方法,访问 https://qyapi.weixin.qq.com/cgi-bin/gettoke ...

  7. Python自动化学习--批量执行.py用例

    这段时间在摸索自动化,学到执行测试用例的时候发现,执行单用例的时候很简单,如果想多条用例执行的话就没那么简单了,经过几番查找,找到如下方法: unittest模块中的TestLoader类有一个dis ...

  8. tp5 微信授权

    protected $appid = '****************'; //微信 appidprotected $appsecrt = '******************'; //微信 ap ...

  9. 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)

    链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...

  10. zookeeper之二 zkCli客户端命令

    ZooKeeper命令行界面(CLI)用于与ZooKeeper集合进行交互以进行开发.它有助于调试和解决不同的选项.要执行ZooKeeper CLI操作,首先打开ZooKeeper服务器(“bin/z ...