深入理解vue中Props屬性
阿新 • • 發佈:2018-10-14
mss command mman 理解 多個 prop 重復 item cin
使用 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-在中間加入—他前面子組件後面加上,後面的子組件後面加上
深入理解vue中Props屬性