简单理解vue中Props属性

 更新时间:2021年04月12日 09:59:03   作者:马优晨  
这篇文章主要帮助大家简单的理解vue中Props属性,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
如果你想靠AI翻身,你先需要一个靠谱的工具!

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

使用 Props 传递数据

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

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

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

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

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

举例

错误写法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!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>

正确写法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!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

1
2
3
4
5
<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
<child nisha="hello2!"></child>
</div>

JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

1
2
3
4
5
<div id="app1">
<child msg="hello!"></child>
 <child nihao="hello1!"></child>
 <child nisha="hello2!"></child>
</div>

JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

1
2
3
4
5
<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
 <child nisha="hello2!"></child>
</div>

JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

1
2
3
4
5
<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
<child nisha="hello2!"></child>
</div>

JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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组件学习教程进行学习。

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

蓄力AI

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

相关文章

  • vue中,在本地缓存中读写数据的方法

    vue中,在本地缓存中读写数据的方法

    今天小编就为大家分享一篇vue中,在本地缓存中读写数据的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • vue3封装自己的分页组件

    vue3封装自己的分页组件

    这篇文章主要为大家详细介绍了vue3封装自己的分页组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Vue学习之Vuex的使用详解

    Vue学习之Vuex的使用详解

    这篇文章主要介绍了Vue中的插件:Vuex。本文将围绕它的优缺点、使用场景和示例展开详细的说明,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-01-01
  • Vue3.0监听器watch与watchEffect详解

    Vue3.0监听器watch与watchEffect详解

    在 Vue 3 中,watch 仍然是一种用于监听数据变化并执行相应操作的方式,不过在组合式 API 中,watch 的使用方式与选项式 API 略有不同,这篇文章主要介绍了Vue3.0监听器watch与watchEffect,需要的朋友可以参考下
    2023-12-12
  • 浅谈vue单页面中有多个echarts图表时的公用代码写法

    浅谈vue单页面中有多个echarts图表时的公用代码写法

    这篇文章主要介绍了浅谈vue单页面中有多个echarts图表时的公用代码写法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • vue-cli —— 如何局部修改Element样式

    vue-cli —— 如何局部修改Element样式

    这篇文章主要介绍了vue如何局部修改Element样式,帮助大家更好的理解和使用vue,感兴趣的朋友可以了解下
    2020-10-10
  • vue前端更新后需要清空缓存代码示例

    vue前端更新后需要清空缓存代码示例

    这篇文章主要给大家介绍了关于vue前端更新后需要清空缓存的相关资料,文中通过代码介绍的非常详细,对大家学习或者使用vue具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-10-10
  • 详解如何在vue项目中使用eslint+prettier格式化代码

    详解如何在vue项目中使用eslint+prettier格式化代码

    在开发中我们需要一种能够统一团队代码风格的工具,作为强制性的规范,统一整个项目的代码风格,这篇文章主要介绍了详解如何在vue项目中使用eslint+prettier格式化代码,需要的朋友可以参考下
    2018-11-11
  • 微前端qiankun主应用与子应用之间的跳转示例

    微前端qiankun主应用与子应用之间的跳转示例

    这篇文章主要为大家介绍了微前端qiankun主应用与子应用之间的跳转示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • vue脚手架中配置Sass的方法

    vue脚手架中配置Sass的方法

    本篇文章主要介绍了vue脚手架中配置Sass的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01

最新评论