简单理解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 HashTable
Jan 22 Javascript
JSONP 跨域共享信息
Aug 16 Javascript
jquery实现页面图片等比例放大缩小功能
Feb 12 Javascript
jQuery事件处理的特征(事件命名机制)
Aug 23 Javascript
js异步编程小技巧详解
Aug 14 Javascript
ES6 javascript的异步操作实例详解
Oct 30 Javascript
jquery写出PC端轮播图实例
Jan 26 jQuery
学习React中ref的两个demo示例
Aug 14 Javascript
Vue中 key keep-alive的实现原理
Sep 18 Javascript
iview tabs 顶部导航栏和模块切换栏的示例代码
Mar 04 Javascript
基于JavaScript或jQuery实现网站夜间/高亮模式
May 30 jQuery
vue路由切换时取消之前的所有请求操作
Sep 01 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
php生成WAP页面
2006/10/09 PHP
一个完整的PHP类包含的七种语法说明
2015/06/04 PHP
Laravel关联模型中过滤结果为空的结果集(has和with区别)
2018/10/18 PHP
Thinkphp框架+Layui实现图片/文件上传功能分析
2020/02/07 PHP
JS运行耗时操作的延时显示方法
2010/11/19 Javascript
jquery提升性能最佳实践小结
2010/12/06 Javascript
js获取当前日期代码适用于网页头部
2013/06/27 Javascript
js实现内容显示并使用json传输数据
2016/03/16 Javascript
Js 获取、判断浏览器版本信息的简单方法
2016/08/08 Javascript
Node.js  REPL (交互式解释器)实例详解
2017/08/06 Javascript
小程序scroll-view组件实现滚动的示例代码
2018/09/20 Javascript
JavaScript实现简易计算器小功能
2020/10/22 Javascript
element el-table表格的二次封装实现(附表格高度自适应)
2021/01/19 Javascript
vue.js实现点击图标放大离开时缩小的代码
2021/01/27 Vue.js
详解Python中break语句的用法
2015/05/14 Python
基于Python 的进程管理工具supervisor使用指南
2016/09/18 Python
你应该知道的python列表去重方法
2017/01/17 Python
Python实现的查询mysql数据库并通过邮件发送信息功能
2018/05/17 Python
Django项目中包含多个应用时对url的配置方法
2018/05/30 Python
Django-Rest-Framework 权限管理源码浅析(小结)
2018/11/12 Python
详解如何用django实现redirect的几种方法总结
2018/11/22 Python
python中partial()基础用法说明
2018/12/30 Python
python 实现GUI(图形用户界面)编程详解
2019/07/17 Python
python实现局域网内实时通信代码
2019/12/22 Python
Python关于__name__属性的含义和作用详解
2020/02/19 Python
德国足球商店:OUTFITTER
2019/05/06 全球购物
文案策划求职信
2014/04/14 职场文书
井冈山红色之旅心得体会
2014/10/07 职场文书
故宫导游词
2015/01/31 职场文书
母亲节寄语大全
2015/02/27 职场文书
综合办公室主任岗位职责
2015/04/01 职场文书
中秋节祝酒词
2015/08/12 职场文书
mysql timestamp比较查询遇到的坑及解决
2021/11/27 MySQL
golang连接MySQl使用sqlx库
2022/04/14 Golang
使用Python开发冰球小游戏
2022/04/30 Python
详细介绍MySQL中limit和offset的用法
2022/05/06 MySQL