简单理解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 相关文章推荐
建议大家看下JavaScript重要知识更新
Jul 08 Javascript
js 实现无干扰阴影效果 简单好用(附文件下载)
Dec 27 Javascript
基于jQuery实现的Ajax 验证用户名是否存在的实现代码
Apr 06 Javascript
js动态设置div的值下例子
Oct 29 Javascript
js数字转换为float,取N位小数
Feb 08 Javascript
基于jQuery仿淘宝产品图片放大镜特效
Oct 19 Javascript
使用vue.js开发时一些注意事项
Apr 27 Javascript
js获取html页面代码中图片地址的实现代码
Mar 05 Javascript
Vue-router 中hash模式和history模式的区别
Jul 24 Javascript
微信小程序实现星级评价
Nov 20 Javascript
vue-router 路由传参用法实例分析
Mar 06 Javascript
在vue中使用cookie记住用户上次选择的实例(本次例子中为下拉框)
Sep 11 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
通过ICQ网关发送手机短信的PHP源程序
2006/10/09 PHP
基于qmail的完整WEBMAIL解决方案安装详解
2006/10/09 PHP
php中通过虚代理实现延迟加载的实现代码
2011/06/10 PHP
jQuery 过滤not()与filter()实例代码
2012/05/10 Javascript
jQuery(非HTML5)可编辑表格实现代码
2012/12/11 Javascript
购物车选中得到价格实现示例
2014/01/26 Javascript
jquery 中ajax执行的优先级
2015/06/22 Javascript
深入理解JavaScript程序中内存泄漏
2016/03/17 Javascript
两种方法解决javascript url post 特殊字符转义 + &amp; #
2016/04/13 Javascript
BootStrap制作导航条实例代码
2016/05/06 Javascript
JS异步文件上传(兼容IE8+)
2017/04/02 Javascript
JavaScript数据结构之二叉树的查找算法示例
2017/04/13 Javascript
javascript简单链式调用案例分析
2017/05/10 Javascript
JS判断数组那点事
2017/10/10 Javascript
JavaScript实现图片的放大缩小及拖拽功能示例
2019/05/14 Javascript
Vue组件模板及组件互相引用代码实例
2020/03/11 Javascript
浅谈使用nodejs搭建web服务器的过程
2020/07/20 NodeJs
跟老齐学Python之集合的关系
2014/09/24 Python
Python动态加载模块的3种方法
2014/11/22 Python
Pandas之DataFrame对象的列和索引之间的转化
2019/06/25 Python
Python assert关键字原理及实例解析
2019/12/13 Python
完美解决pycharm导入自己写的py文件爆红问题
2020/02/12 Python
Python批量将图片灰度化的实现代码
2020/04/11 Python
python 如何设置守护进程
2020/10/29 Python
html5本地存储 localStorage操作使用详解
2016/09/20 HTML / CSS
科颜氏美国官网:Kiehl’s美国
2017/01/31 全球购物
Nordgreen手表德国官方网站:丹麦极简主义手表
2019/10/31 全球购物
应届毕业生应聘自荐信
2013/12/07 职场文书
网上蛋糕店创业计划书
2014/01/24 职场文书
学生手册家长评语
2014/02/10 职场文书
最经典的大学生职业生涯规划范文
2014/03/05 职场文书
表扬通报怎么写
2015/01/16 职场文书
毕业实习计划书
2015/01/16 职场文书
人事行政助理岗位职责
2015/04/11 职场文书
SQL CASE 表达式的具体使用
2022/03/21 SQL Server
阿里云服务器部署RabbitMQ集群的详细教程
2022/06/01 Servers