props写法_简单理解vue中Props属性

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

使用 Props 传递数据

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

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

Vue.component(‘child’, {

// 声明 props

props: ['msg'],

// prop 可以用在模板内

// 可以用 `this.msg` 设置

template: ’{{ msg }}’

})

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

<

ild>

举例

错误写法:

html>

vue.js
 
 

//使用 props 传输资料予子组件

//props , data 重复名称会出现错误

<

ild>

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: ’{{ msg }}{{nihao}}{{nisha}}’,

data: function() {

return {

mssage: ’boy’

}

}

});

var vm = new Vue({

el: ’#app1′

})

<

ml>

正确写法:

html>

vue.js
 
 

//使用 props 传输资料予子组件

//props , data 重复名称会出现错误

<

ild>

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: ’{{ msg }}{{nihao}}{{nisha}}’

});

var vm = new Vue({

el: ’#app1′

})

<

ml>

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

第一种:

HTML

<

ild>

<

ild>

<

ild>

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: ’{{ msg }}{{nihao}}{{nisha}}’,

/*data: function() {

return {

msg: ’boy’

}

}*/

});

var vm = new Vue({

el: ’#app1′

})

结果:hello! hello1! hello2!

第二种:

HTML

<

ild>

<

ild>

<

ild>

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: ’123{{ msg }}{{nihao}}{{nisha}}’,

/*data: function() {

return {

msg: ’boy’

}

}*/

});

var vm = new Vue({

el: ’#app1′

})

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

第三种:

HTML

<

ild>

<

ild>

<

ild>

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: ’{{ msg }}{{nihao}}{{nisha}}123’,

/*data: function() {

return {

msg: ’boy’

}

}*/

});

var vm = new Vue({

el: ’#app1′

})

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

第四种:

HTML

<

ild>

<

ild>

<

ild>

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: ’{{ msg }}123{{nihao}}{{nisha}}123’,

/*data: function() {

return {

msg: ’boy’

}

}*/

});

var vm = new Vue({

el: ’#app1′

})

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

结论:

在props 中传入多个数据是,如果在父组件的模板类添加其他元素或者字符会有:

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值