国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

簡單理解vue中Props屬性

2019-11-20 08:39:19
字體:
來源:轉載
供稿:網友

本文實例為大家解析了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組件學習教程進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 谷城县| 大宁县| 和龙市| 绿春县| 桃园市| 方正县| 商城县| 龙南县| 杂多县| 嘉黎县| 阜平县| 哈巴河县| 江都市| 府谷县| 密山市| 菏泽市| 柳江县| 芮城县| 特克斯县| 航空| 合作市| 洛扎县| 利川市| 永修县| 宣武区| 五大连池市| 广水市| 太白县| 乌恰县| 米泉市| 连州市| 宣城市| 千阳县| 广东省| 尼木县| 泸定县| 霍邱县| 临湘市| 资阳市| 祁连县| 西乡县|