HTML5¼¼Êõ

Vue.js60·ÖÖÓ×é¼þ¿ìËÙÈëÃÅ£¨ÉÏƪ£© - keepfool(3)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£º²©¿ÍÔ° 2016-07-01 14:00 ÎÒÒªÆÀÂÛ( )

ÁíÍ⣬Èç¹ûdataÑ¡ÏîÖ¸Ïòij¸ö¶ÔÏó£¬ÕâÒâζ×ÅËùÓеÄ×é¼þʵÀý¹²ÓÃÒ»¸ödata¡£ ÎÒÃÇÓ¦µ±Ê¹ÓÃÒ»¸öº¯Êý×÷Ϊ data Ñ¡ÏÈÃÕâ¸öº¯Êý·µ»ØÒ»¸öжÔÏó£º Vue.component('my-component', {data: function(){return {a : 1}}})ʹ

ÁíÍ⣬Èç¹ûdataÑ¡ÏîÖ¸Ïòij¸ö¶ÔÏó£¬ÕâÒâζ×ÅËùÓеÄ×é¼þʵÀý¹²ÓÃÒ»¸ödata¡£
ÎÒÃÇÓ¦µ±Ê¹ÓÃÒ»¸öº¯Êý×÷Ϊ data Ñ¡ÏÈÃÕâ¸öº¯Êý·µ»ØÒ»¸öжÔÏó£º

Vue.component('my-component', { data: function(){ return {a : 1} } }) ʹÓÃprops

×é¼þʵÀýµÄ×÷ÓÃÓòÊǹÂÁ¢µÄ¡£ÕâÒâζ×Ų»Äܲ¢ÇÒ²»Ó¦¸ÃÔÚ×Ó×é¼þµÄÄ£°åÄÚÖ±½ÓÒýÓø¸×é¼þµÄÊý¾Ý¡£¿ÉÒÔʹÓà 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

Õâ¶Î³ÌÐòµÄÔËÐнá¹ûÈçÏ£º

image

¸¸×é¼þÊÇÈçºÎ½«Êý¾Ý´«¸ø×Ó×é¼þµÄÄØ£¿ÏàÐÅ¿´ÁËÏÂÃæÕâͼ£¬Ò²ÐíÄã¾ÍÄܺܺõØÀí½âÁË¡£

image

ÔÚ¸¸×é¼þÖÐʹÓÃ×Ó×é¼þʱ£¬Í¨¹ýÒÔÏÂÓï·¨½«Êý¾Ý´«µÝ¸ø×Ó×é¼þ£º

<child-component v-bind:×Ó×é¼þprop="¸¸×é¼þÊý¾ÝÊôÐÔ"></child-component> propµÄ°ó¶¨ÀàÐÍ µ¥Ïò°ó¶¨

¼ÈÈ»¸¸×é¼þ½«Êý¾Ý´«µÝ¸øÁË×Ó×é¼þ£¬ÄÇôÈç¹û×Ó×é¼þÐÞ¸ÄÁËÊý¾Ý£¬¶Ô¸¸×é¼þÊÇ·ñ»áÓÐËùÓ°ÏìÄØ£¿
ÎÒÃǽ«×Ó×é¼þÄ£°åºÍÒ³ÃæHTMLÉÔ×÷¸ü¸Ä£º

<div> <table> <tr> <th colspan="3">¸¸×é¼þÊý¾Ý</td> </tr> <tr> <td>name</td> <td>{{ name }}</td> <td><input type="text" v-model="name" /></td> </tr> <tr> <td>age</td> <td>{{ age }}</td> <td><input type="text" v-model="age" /></td> </tr> </table> <my-component v-bind:my-name="name" v-bind:my-age="age"></my-component> </div> <template> <table> <tr> <th colspan="3">×Ó×é¼þÊý¾Ý</td> </tr> <tr> <td>my name</td> <td>{{ myName }}</td> <td><input type="text" v-model="myName" /></td> </tr> <tr> <td>my age</td> <td>{{ myAge }}</td> <td><input type="text" v-model="myAge" /></td> </tr> </table> </template>

View Demo

ÔËÐÐÕâ¸öÒ³Ã棬ÎÒÃÇ×öÁ½¸öСÊÔÑ飺

1. ÔÚÒ³ÃæÉÏÐÞ¸Ä×Ó×é¼þµÄÊý¾Ý

7

ÐÞ¸ÄÁË×Ó×é¼þµÄÊý¾Ý£¬Ã»ÓÐÓ°Ï츸×é¼þµÄÊý¾Ý¡£

2. ÔÚÒ³ÃæÉÏÐ޸ĸ¸×é¼þµÄÊý¾Ý

8

ÐÞ¸ÄÁ˸¸×é¼þµÄÊý¾Ý£¬Í¬Ê±Ó°ÏìÁË×Ó×é¼þ¡£

propĬÈÏÊǵ¥Ïò°ó¶¨£ºµ±¸¸×é¼þµÄÊôÐԱ仯ʱ£¬½«´«µ¼¸ø×Ó×é¼þ£¬µ«ÊÇ·´¹ýÀ´²»»á¡£ÕâÊÇΪÁË·ÀÖ¹×Ó×é¼þÎÞÒâÐÞ¸ÄÁ˸¸×é¼þµÄ״̬

Ë«Ïò°ó¶¨

¿ÉÒÔʹÓÃ.syncÏÔʽµØÖ¸¶¨Ë«Ïò°ó¶¨£¬ÕâʹµÃ×Ó×é¼þµÄÊý¾ÝÐ޸Ļá»Ø´«¸ø¸¸×é¼þ¡£

<my-component v-bind:my-name.sync="name" v-bind:my-age.sync="age"></my-component>

9

View Deom

µ¥´Î°ó¶¨

¿ÉÒÔʹÓÃ.onceÏÔʽµØÖ¸¶¨µ¥´Î°ó¶¨£¬µ¥´Î°ó¶¨ÔÚ½¨Á¢Ö®ºó²»»áͬ²½Ö®ºóµÄ±ä»¯£¬ÕâÒâζ׿´Ê¹¸¸×é¼þÐÞ¸ÄÁËÊý¾Ý£¬Ò²²»»á´«µ¼¸ø×Ó×é¼þ¡£

<my-component v-bind:my-name.once="name" v-bind:my-age.once="age"></my-component>

10

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¹ýÂËÆ÷

¿ÉÒÔ¸ù¾ÝÖ¸¶¨µÄ×Ö·û´®¹ýÂËÊý¾Ý¡£

11

×ܽá

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • OmiÊ÷×é¼þomi-tree±àдָÄÏ - ¡¾µ±ÄÍÌØ¡¿

    OmiÊ÷×é¼þomi-tree±àдָÄÏ - ¡¾µ±ÄÍÌØ¡¿

    2017-05-02 15:04

  • ÔÚDelphiÏÂʹÓÃѸÀ×APlayer×é¼þ½øÐÐÃâ×¢²á¿ª·¢ - DelphiÁ¦Á¿

    ÔÚDelphiÏÂʹÓÃѸÀ×APlayer×é¼þ½øÐÐÃâ×¢²á¿ª·¢ - DelphiÁ¦Á¿

    2017-04-28 15:00

  • JS×é¼þϵÁСª¡ª×Ô¼º¶¯ÊÖ·â×°bootstrap-treegrid×é¼þ - ÀÁµÃ°²·Ö

    JS×é¼þϵÁСª¡ª×Ô¼º¶¯ÊÖ·â×°bootstrap-treegrid×é¼þ - ÀÁµÃ°²·Ö

    2017-04-28 14:02

  • ±í¸ñ×é¼þÉñÆ÷£ºbootstrap tableÏêϸʹÓÃÖ¸ÄÏ - Á÷À˵ÄÊ«ÈË

    ±í¸ñ×é¼þÉñÆ÷£ºbootstrap tableÏêϸʹÓÃÖ¸ÄÏ - Á÷À˵ÄÊ«ÈË

    2017-04-13 09:00

ÍøÓѵãÆÀ
Ô