简单理解vue中Props属性


Posted in Javascript onOctober 27, 2016

本文实例为大家解析了vue中Props的属性,供大家参考,具体内容如下

使用 Props 传递数据

组件实例的作用域是孤立的。这意味着不能并且不应该在子组件的模板内直接引用父组件的数据。可以使用 props 把数据传给子组件。

“prop” 是组件数据的一个字段,期望从父组件传下来。子组件需要显式地用 props 选项 声明 props:

Vue.component('child', {
 // 声明 props
 props: ['msg'],
 // prop 可以用在模板内
 // 可以用 `this.msg` 设置
 template: '<span>{{ msg }}</span>'
})

然后向它传入一个普通字符串:

<child msg="hello!"></child>

举例

错误写法:

<!DOCTYPE html>
<html lang="en">

<head>
 <script type="text/javascript" src="./vue.js"></script>
 <meta charset="UTF-8">
 <title>vue.js</title>
</head>

<body>
<pre>
 //使用 props 传输资料予子组件
 //props , data 重复名称会出现错误


</pre>
<div id="app1">
 <child mssage="hello!"></child>
</div>
<script>
 Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}{{nihao}}{{nisha}}</span>',
 data: function() {
 return {
 mssage: 'boy'
 }
 }
 });
 var vm = new Vue({
 el: '#app1'
 })
</script>
</body>

</html>

正确写法:

<!DOCTYPE html>
<html lang="en">

<head>
 <script type="text/javascript" src="./vue.js"></script>
 <meta charset="UTF-8">
 <title>vue.js</title>
</head>

<body>
<pre>
 //使用 props 传输资料予子组件
 //props , data 重复名称会出现错误


</pre>
<div id="app1">
 <child mssage="hello!"></child>
</div>
<script>
 Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}{{nihao}}{{nisha}}</span>'
 });
 var vm = new Vue({
 el: '#app1'
 })
</script>
</body>

</html>

props 传入多个数据(顺序问题)

第一种:

HTML             

<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
<child nisha="hello2!"></child>
</div>

JS

Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}{{nihao}}{{nisha}}</span>',
 /*data: function() {
 return {
 msg: 'boy'
 }
 }*/
 });
 var vm = new Vue({
 el: '#app1'
 })

结果:hello! hello1! hello2!

第二种:

HTML

<div id="app1">
<child msg="hello!"></child>
 <child nihao="hello1!"></child>
 <child nisha="hello2!"></child>
</div>

JS

Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>123{{ msg }}{{nihao}}{{nisha}}</span>',
 /*data: function() {
 return {
 msg: 'boy'
 }
 }*/
 });
 var vm = new Vue({
 el: '#app1'
 })

结果:123hello! 123hello1! 123hello2!

第三种:

HTML

<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
 <child nisha="hello2!"></child>
</div>

JS

Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}{{nihao}}{{nisha}}123</span>',
 /*data: function() {
 return {
 msg: 'boy'
 }
 }*/
 });
 var vm = new Vue({
 el: '#app1'
 })

结果:hello! 123 hello1! 123 hello2!123

第四种:

HTML                 

<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
<child nisha="hello2!"></child>
</div>

JS

Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}123{{nihao}}{{nisha}}123</span>',
 /*data: function() {
 return {
 msg: 'boy'
 }
 }*/
 });
 var vm = new Vue({
 el: '#app1'
 })

结果:hello! 123 123hello1! 123hello2!

结论:

在props 中传入多个数据是,如果在父组件的模板类添加其他元素或者字符会有:
1-在最前面加入—每个子组件渲染出来都会在其前面加上

2-在最后面加入—每个子组件渲染出来都会在其后面加上

3-在中间加入—他前面子组件后面加上,后面的子组件后面加上

参考: http://cn.vuejs.org/guide/components.html#Props

本文已被整理到了《Vue.js前端组件学习教程》,欢迎大家学习阅读。

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
基于jquery的15款幻灯片插件
Apr 10 Javascript
jQuery中:lt选择器用法实例
Dec 29 Javascript
JS基于FileSystemObject创建一个指定路径的TXT文本文件
Aug 05 Javascript
bootstrap组件之按钮式下拉菜单小结
Jan 19 Javascript
js实现瀑布流效果(自动生成新的内容)
Mar 16 Javascript
ionic3+Angular4实现接口请求及本地json文件读取示例
Oct 11 Javascript
vue2.0设置proxyTable使用axios进行跨域请求的方法
Oct 19 Javascript
koa socket即时通讯的示例代码
Sep 07 Javascript
vue  elementUI 表单嵌套验证的实例代码
Nov 06 Javascript
解决vue项目中某一页面不想引用公共组件app.vue的问题
Aug 14 Javascript
Vue实现菜单切换功能
Nov 08 Javascript
如何使用原生Js实现随机点名详解
Jan 06 Javascript
利用React-router+Webpack快速构建react程序
Oct 27 #Javascript
如何使用Vuex+Vue.js构建单页应用
Oct 27 #Javascript
浅析如何利用JavaScript进行语音识别
Oct 27 #Javascript
javascript鼠标跟随运动3种效果(眼球效果,苹果菜单,方向跟随)
Oct 27 #Javascript
简单理解vue中track-by属性
Oct 26 #Javascript
javascript iframe跨域详解
Oct 26 #Javascript
JS日期对象简单操作(获取当前年份、星期、时间)
Oct 26 #Javascript
You might like
2020年4月放送决定!第2期TV动画《邪神酱飞踢》视觉图&主题曲情报公开!
2020/03/06 日漫
JS宝典学习笔记(下)
2007/01/10 Javascript
JS控制阿拉伯数字转为中文大写示例代码
2013/09/04 Javascript
Javascript前端UI框架Kit使用指南之Kitjs简介
2014/11/28 Javascript
Jquery 实现弹出层插件
2015/01/28 Javascript
JavaScript简介
2015/02/15 Javascript
简介JavaScript中用于处理正切的Math.tan()方法
2015/06/15 Javascript
javascript+canvas实现刮刮卡抽奖效果
2015/07/29 Javascript
JS使用eval()动态创建变量的方法
2016/06/03 Javascript
AngularJS中的路由使用及实现代码
2017/10/09 Javascript
vue中实现拖动调整左右两侧div的宽度的示例代码
2020/07/22 Javascript
[36:37]2014 DOTA2华西杯精英邀请赛5 24 VG VS iG
2014/05/25 DOTA
python中readline判断文件读取结束的方法
2014/11/08 Python
python判断一个集合是否包含了另外一个集合中所有项的方法
2015/06/30 Python
详解Python的Twisted框架中reactor事件管理器的用法
2016/05/25 Python
python中hashlib模块用法示例
2017/10/30 Python
人脸识别经典算法一 特征脸方法(Eigenface)
2018/03/13 Python
Django框架之DRF 基于mixins来封装的视图详解
2019/07/23 Python
Python FFT合成波形的实例
2019/12/04 Python
Python字符串hashlib加密模块使用案例
2020/03/10 Python
Python 利用flask搭建一个共享服务器的步骤
2020/12/05 Python
python实现PolynomialFeatures多项式的方法
2021/01/06 Python
挪威户外活动服装和装备购物网站:Bergfreunde挪威
2016/10/20 全球购物
一套Delphi的笔试题二
2013/05/11 面试题
2014年社区植树节活动方案
2014/02/28 职场文书
应用数学专业求职信
2014/03/14 职场文书
绿色城市实施方案
2014/03/19 职场文书
新品发布会主持词
2014/04/02 职场文书
《记金华的双龙洞》教学反思
2014/04/19 职场文书
中学生演讲稿
2014/04/26 职场文书
幼儿园安全生产月活动总结
2014/07/05 职场文书
乡镇党委书记第三阶段个人整改措施
2014/09/16 职场文书
党的群众路线学习笔记
2014/11/06 职场文书
中学团支部工作总结
2015/08/13 职场文书
Java基于字符界面的简易收银台
2021/06/26 Java/Android
nginx lua 操作 mysql
2022/05/15 Servers