ÁíÍ⣬Èç¹ûdataÑ¡ÏîÖ¸Ïòij¸ö¶ÔÏó£¬ÕâÒâζ×ÅËùÓеÄ×é¼þʵÀý¹²ÓÃÒ»¸ödata¡£
ÎÒÃÇÓ¦µ±Ê¹ÓÃÒ»¸öº¯Êý×÷Ϊ data Ñ¡ÏÈÃÕâ¸öº¯Êý·µ»ØÒ»¸öжÔÏó£º
×é¼þʵÀýµÄ×÷ÓÃÓòÊǹÂÁ¢µÄ¡£ÕâÒâζ×Ų»Äܲ¢ÇÒ²»Ó¦¸ÃÔÚ×Ó×é¼þµÄÄ£°åÄÚÖ±½ÓÒýÓø¸×é¼þµÄÊý¾Ý¡£¿ÉÒÔʹÓà props °ÑÊý¾Ý´«¸ø×Ó×é¼þ¡£
props»ù´¡Ê¾ÀýÏÂÃæµÄ´úÂ붨ÒåÁËÒ»¸ö×Ó×é¼þmy-component£¬ÔÚVueʵÀýÖж¨ÒåÁËdataÑ¡Ïî¡£
var vm = new Vue({ el: '#app', data: { name: 'keepfool', age: 28 }, components: { 'my-component': { template: '#myComponent', props: ['myName', 'myAge'] } } })ΪÁ˱ãÓÚÀí½â£¬Äã¿ÉÒÔ½«Õâ¸öVueʵÀý¿´×÷my-componentµÄ¸¸×é¼þ¡£
Èç¹ûÎÒÃÇÏëʹ¸¸×é¼þµÄÊý¾Ý£¬Ôò±ØÐëÏÈÔÚ×Ó×é¼þÖж¨ÒåpropsÊôÐÔ£¬Ò²¾ÍÊÇprops: ['myName', 'myAge']ÕâÐдúÂë¡£
¶¨Òå×Ó×é¼þµÄHTMLÄ£°å£º
<template> <table> <tr> <th colspan="2"> ×Ó×é¼þÊý¾Ý </th> </tr> <tr> <td>my name</td> <td>{{ myName }}</td> </tr> <tr> <td>my age</td> <td>{{ myAge }}</td> </tr> </table> </template>½«¸¸×é¼þÊý¾Ýͨ¹ýÒѶ¨ÒåºÃµÄpropsÊôÐÔ´«µÝ¸ø×Ó×é¼þ£º
<div> <my-component v-bind:my-name="name" v-bind:my-age="age"></my-component> </div>×¢Ò⣺ÔÚ×Ó×é¼þÖж¨Òåpropʱ£¬Ê¹ÓÃÁËcamelCaseÃüÃû·¨¡£ÓÉÓÚHTMLÌØÐÔ²»Çø·Ö´óСд£¬camelCaseµÄpropÓÃÓÚÌØÐÔʱ£¬ÐèҪתΪ kebab-case£¨¶ÌºáÏ߸ô¿ª£©¡£ÀýÈ磬ÔÚpropÖж¨ÒåµÄmyName£¬ÔÚÓÃ×÷ÌØÐÔʱÐèҪת»»Îªmy-name¡£
View Demo
Õâ¶Î³ÌÐòµÄÔËÐнá¹ûÈçÏ£º
¸¸×é¼þÊÇÈçºÎ½«Êý¾Ý´«¸ø×Ó×é¼þµÄÄØ£¿ÏàÐÅ¿´ÁËÏÂÃæÕâͼ£¬Ò²ÐíÄã¾ÍÄܺܺõØÀí½âÁË¡£
ÔÚ¸¸×é¼þÖÐʹÓÃ×Ó×é¼þʱ£¬Í¨¹ýÒÔÏÂÓï·¨½«Êý¾Ý´«µÝ¸ø×Ó×é¼þ£º
<child-component v-bind:×Ó×é¼þprop="¸¸×é¼þÊý¾ÝÊôÐÔ"></child-component> propµÄ°ó¶¨ÀàÐÍ µ¥Ïò°ó¶¨¼ÈÈ»¸¸×é¼þ½«Êý¾Ý´«µÝ¸øÁË×Ó×é¼þ£¬ÄÇôÈç¹û×Ó×é¼þÐÞ¸ÄÁËÊý¾Ý£¬¶Ô¸¸×é¼þÊÇ·ñ»áÓÐËùÓ°ÏìÄØ£¿
ÎÒÃǽ«×Ó×é¼þÄ£°åºÍÒ³ÃæHTMLÉÔ×÷¸ü¸Ä£º
View Demo
ÔËÐÐÕâ¸öÒ³Ã棬ÎÒÃÇ×öÁ½¸öСÊÔÑ飺
1. ÔÚÒ³ÃæÉÏÐÞ¸Ä×Ó×é¼þµÄÊý¾Ý
ÐÞ¸ÄÁË×Ó×é¼þµÄÊý¾Ý£¬Ã»ÓÐÓ°Ï츸×é¼þµÄÊý¾Ý¡£
2. ÔÚÒ³ÃæÉÏÐ޸ĸ¸×é¼þµÄÊý¾Ý
ÐÞ¸ÄÁ˸¸×é¼þµÄÊý¾Ý£¬Í¬Ê±Ó°ÏìÁË×Ó×é¼þ¡£
propĬÈÏÊǵ¥Ïò°ó¶¨£ºµ±¸¸×é¼þµÄÊôÐԱ仯ʱ£¬½«´«µ¼¸ø×Ó×é¼þ£¬µ«ÊÇ·´¹ýÀ´²»»á¡£ÕâÊÇΪÁË·ÀÖ¹×Ó×é¼þÎÞÒâÐÞ¸ÄÁ˸¸×é¼þµÄ״̬
Ë«Ïò°ó¶¨¿ÉÒÔʹÓÃ.syncÏÔʽµØÖ¸¶¨Ë«Ïò°ó¶¨£¬ÕâʹµÃ×Ó×é¼þµÄÊý¾ÝÐ޸Ļá»Ø´«¸ø¸¸×é¼þ¡£
<my-component v-bind:my-name.sync="name" v-bind:my-age.sync="age"></my-component>View Deom
µ¥´Î°ó¶¨¿ÉÒÔʹÓÃ.onceÏÔʽµØÖ¸¶¨µ¥´Î°ó¶¨£¬µ¥´Î°ó¶¨ÔÚ½¨Á¢Ö®ºó²»»áͬ²½Ö®ºóµÄ±ä»¯£¬ÕâÒâζ׿´Ê¹¸¸×é¼þÐÞ¸ÄÁËÊý¾Ý£¬Ò²²»»á´«µ¼¸ø×Ó×é¼þ¡£
<my-component v-bind:my-name.once="name" v-bind:my-age.once="age"></my-component>View Deom
ʾÀýΪÁ˾¡¿ìÏû»¯ÕâЩ֪ʶ£¬ÎÒÃÇÀ´×öÒ»¸öСʾÀý°É¡£
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link href="styles/demo.css" /> </head> <body> <div> <div> Search <input type="text" v-model="searchQuery" /> </div> <simple-grid :data="gridData" :columns="gridColumns" :filter-key="searchQuery"> </simple-grid> </div> <template> <table> <thead> <tr> <th v-for="col in columns"> {{ col | capitalize}} </th> </tr> </thead> <tbody> <tr v-for="entry in data | filterBy filterKey"> <td v-for="col in columns"> {{entry[col]}} </td> </tr> </tbody> </table> </template> </body> <script src="js/vue.js"></script> <script> Vue.component('simple-grid', { template: '#grid-template', props: { data: Array, columns: Array, filterKey: String } }) var demo = new Vue({ el: '#app', data: { searchQuery: '', gridColumns: ['name', 'age', 'sex'], gridData: [{ name: 'Jack', age: 30, sex: 'Male' }, { name: 'Bill', age: 26, sex: 'Male' }, { name: 'Tracy', age: 22, sex: 'Female' }, { name: 'Chris', age: 36, sex: 'Male' }] } }) </script> </html>View Demo
³ýÁËÒÔÉϽéÉܵÄ֪ʶµã£¬Õâ¸öʾÀý»¹Óõ½ÁËÁ½¸ö֪ʶµã£º
1. propÑéÖ¤
props: { data: Array, columns: Array, filterKey: String }Õâ¶Î´úÂë±íʾ£º¸¸×é¼þ´«µÝ¹ýÀ´µÄdataºÍcolumns±ØÐëÊÇArrayÀàÐÍ£¬filterKey±ØÐëÊÇ×Ö·û´®ÀàÐÍ¡£
¸ü¶àpropÑéÖ¤µÄ½éÉÜ£¬Çë²Î¿¼£º
2. filterBy¹ýÂËÆ÷
¿ÉÒÔ¸ù¾ÝÖ¸¶¨µÄ×Ö·û´®¹ýÂËÊý¾Ý¡£
×ܽá
¡¡