Share
Embed
Next Vue.js 0.12
kazupon
May 27, 2015
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
52 slides
-
ࣗݾհ • @kazupon • ׂɿϦʔυΤϯδχΞ • ࣄɿ։ൃશൠ (ϑϩϯτΤϯυ/όοΫΤϯυ/Πϯϑϥ) •
Vue.jsͱͷؔΘΓɿ Vue.js organizationͷϝϯόͱͯ͠vuejs/vue-validator ͷϝϯςφͯ͠·͢ • ࠷ۙͷJSؔ࿈ͷڵຯɿ FRP | Stream | ServiceWorker | nodejs/NG | electron -
͡Ίʹ • ࠓ͓͢Δ༰ɺҎԼͷใͱݱ࣌࠷৽Ͱ͋Δ 0.12βͷಈ࡞֬ೝΛݩʹͨ͠༰Ͱ͢ • Release Tag - https://github.com/yyx990803/vue/releases/tag/0.12.0-beta2 -
https://github.com/yyx990803/vue/releases/tag/0.12.0-beta3 • 0.12 Plan - feedback welcome (vuejs/Discussion) - https://github.com/vuejs/Discussion/issues/158 • ·ͩ։ൃதͳͷͰɺ࠷ऴతʹϦϦʔε͞ΕΔ0.12Ͱɺ ຊൃදͷ༰Λ֬ূͰ͖ΔͷͰ͋Γ·ͤΜ -
0.12ͰۄͳϞϊ • ৽ػೳ • async components • element directives •
༷มߋ • partial • filter • v-with directive • paramAttributes option • v-component directive -
async components • ैདྷͷίϯϙʔωϯτͷ͍ํ Vue.extendͨ͠αϒΫϥε·ͨΦϒδΣΫτΛࢦఆ ͯ͠ಉظతʹొ new Vue({ el: '#app',
data: { a: 1, b: 2 }, components: { child1: Vue.extend({ // case1: Vue.extend template: '<span>child1</span>' }), child2: { // case2: Object template: '<span>child2</span>' } } }) -
async components • 0.12Ҏ߱ͰɺίϯϙʔωϯτͷԆϩʔσΟϯάΛ࣮ ͨؔ͠Λࢦఆ͢Δ͜ͱʹΑͬͯɺΛղܾ͢Δ͜ ͱ͕Ͱ͖Δ new Vue({ el: '#app',
components: { child1: function (resolve) { // load component loadComponent(function (component) { resolve(component) // resolve component !! } } } }) -
async components • ྫ: require.js <!-- HTML --> <div id="app">
<async-child child-msg=“{{message}}"> </async-child> </div> // JS (main) new Vue({ el: '#app', data: { message: 'hello world !!' }, components: { 'async-child': function (resolve) { // with require.js require(['./module1.js'], resolve) } } }) // JS (module1) module.exports = { props: ['child-msg'], template: '<span>{{childMsg}}</span>' } https://jsfiddle.net/kazupon/39mbpaz9/3/ -
async components • ྫ: webpack (code-splitting) <!-- HTML --> <div
id="app"> <async-child child-msg=“{{message}}"> </async-child> </div> // JS (main) new Vue({ el: '#app', data: { message: 'hello world !!' }, components: { 'async-child': function (resolve) { // with webpack require.ensure(['./module1.js'], function (require) { require(['./module1.js'], resolve) } } } }) // JS (module1) module.exports = { props: ['child-msg'], template: '<span>{{childMsg}}</span>' } -
element directives • ैདྷͷdirective/custom direcitvesɺରͱͳ Δλάʹରͯ͠ଐੑతʹࢦఆ͢Δ͜ͱʹΑͬͯػೳΛ Ճ <!-- vuejs/vue-validatorͷΧελϜσΟϨΫςΟϒͷྫ -->
<form id="blog-form"> <input type="text" v-model="comment" v-validate="minLength: 16, maxLength: 128"> <div> <span v-show="validation.comment.minLength">too long your comment.</span> <span v-show="validation.comment.maxLength">too short your comment.</span> </div> <input type="submit" value="send" v-if="valid"> </form> -
element directives • element directivesɺΧελϜλ άશମʹରͯ͠ػೳΛՃͰ͖Δ (AngularͷRestrict Eʹ૬) •
࣮ํ๏custom directivesͱಉ ༷ɺbind/update/unbindΛ࣮ • ԼهΞηοτAPIͰొ - Vue.elementDirective (άϩʔόϧΞηοτ) - elementDirectives (ϓϥΠϕʔτΞηοτ) <!-- HTML --> <div id="app"> <element-directive> <p>{{message}}</p> </element-directive> </div> // JS new Vue({ el: '#app', data: { message: 'hello world !!' }, elementDirectives: { // private 'element-directive': { bind: function () { // ... }, update: function (val, old) { // ... }, unbind: function () { // ... } } } }) -
element directives • custom directivesͱͷҧ͍ 1. argͱexpressionड͚ೖΕෆՄ https://jsfiddle.net/kazupon/gv9wLL94/10/ <!-- HTML
--> <div id="app"> <div> <input type="radio" name="view1" value="view1" v-model="view"> <input type="radio" name="view2" value="view2" v-model="view"> </div> <p>current view : <view>view1</view></p> <div> # element directive ͷҾͷ༰ <p>name: {{name}}</p> <p>arg: {{arg}}</p> <p>expression: {{expression}}</p> <p>raw: {{raw}}</p> </div> </div> new Vue({ data: { view: ‘view1' }, elementDirectives: { view: { bind: function () { var self = this this.vm.$set('arg', this.arg) // undefined this.vm.$set('expression', this.expression) // “” this.vm.$set('raw', this.raw) // “” this.vm.$set('name', this.name) // view this._watcher = this.vm.$watch('view', function (val, old) { self.update(val, old) }) }, ... -
element directives • custom directivesͱͷҧ͍ 2. λʔϛφϧͳσΟϨΫςΟϒͳͷͰɺΧελϜλάʹ͋Δࢠͷλάʹ ରͯ͠ίϯύΠϧॲཧ͕εΩοϓ͞ΕΔ ͭ·Γ
- ΧελϜλάΛDOMΛͬͯࣗ༝ʹૢ࡞͢Δ͜ͱ͕Ͱ͖Δ - VueίϯϙʔωϯτͷΠϯελϯεੜͷΦʔόʔϔου͕ͳ͍ ͳͷͰɺ ύϑΥʔϚϯε͕ٻΊΒΕΔίϯϙʔωϯτΛ࡞Δʹɺίϯϙʔωϯτγ εςϜͷΦϧλφςΟϒͳͷͱͯ͠ར༻Ͱ͖Δ -
partial • v-partial directiveͱ{{>partial}}ϒϩοΫͷഇ ࢭ •
͜Εʹ͍ɺpartialΛొ͢ΔΞηοτAPIআ (άϩʔόϧɺϓϥΠϕʔτ྆ํ) <!-- v-partial directive --> <div v-partial="{{partialId}}"></div> <!-- partial block --> <div>{{>my-partial}}</div> -
filter • ϑΟϧλҾʹจࣈྻΛ͢߹ɺγϯάϧΫΥʔτ (‘’)ඞਢ <!-- 0.11 --> <input v-on="keyup: submitForm
| key enter"> <!-- 0.12 later --> <input v-on="keyup: submitForm | key 'enter'"> -
filter • $dataΛͬͨಈతͳϑΟϧλҾͷαϙʔτ https://jsfiddle.net/kazupon/q2kykpuz/ <!-- HTML --> <div id="app"> <form>
<input type="text" v-model="first"> <input type="text" v-model="last"> </form> <div> <span>{{first | concat last}}</span> </div> </div> // JS new Vue({ data: { first: '', last: '' }, filters: { concat: function (val, arg) { return val + arg } } }).$mount('#app') -
v-with directive • v-with directiveഇࢭ <!-- HTML --> <div id="app">
<p v-component="user-profile" v-with="user"></p> </div>iv> // JS Vue.component('user-profile', { template: '{{name}}<br>{{email}}' }) var parent = new Vue({ el: '#app', data: { user: { name: 'Foo Bar', email: '[email protected]' } } -
v-with directive • ςϯϓϨʔτͷ{{#with}}ϒϩοΫͰσʔλΛड͚͢ ํ๏ΛσΟϨΫςΟϒԽͨ͠ෛͷҨ࢈ͳͷͰআ͍ͨ͠ <!-- Handlesbars --> <div id=“app">
{{#with user}} <p> {{{name}}}<br>{{{email}}} </p> {{/with}} </div>> -
v-with directive • 0.11ͰσʔλΛɺࢠʹରͯ͠໌ࣔతʹड͚͢ํ๏͕ 2͔ͭ͋ͬͯΓʹ͍͘ # paramAttributes option ʹΑΔํ๏ <!--
HTML --> <div id="app"> <input v-model="parentMsg"> <child-component child-msg="parentMsg"></child-component> </div> // JS new Vue({ el: '#app', data: { parentMsg: 'parent message' }, components: { 'child-component': { paramAttributes: ['child-msg'], template: '<span>{{childMsg}}</span>' } } }) -
v-with directive • 0.12Ҏ߱Ͱࢠͷσʔλͷड͚͠ɺࢠͷprops optionܦ༝Ͱ౷Ұ͢Δ <!-- HTML --> <div id="app">
<input type="text" v-model="parentMsg"> <child child-msg="{{parentMsg}}"></child> </div> // JS new Vue({ el: '#app', data: { parentMsg: 'hello world !!' }, components: { child: { props: ['child-msg'], template: '<span>{{childMsg}}</span>' } } }) https://jsfiddle.net/kazupon/7ph214eg/4/ -
v-with directive • ࢠͷ$dataΛյͯ͠͠·͏ ex1: $dataʹҙਤ͠ͳ͍ϓϩύςΟΛੜͯ͠͠·͏ <!-- HTML --> <div
id="app1"> <p># v-withͳ͠</p> <div v-component="child"></div> </div> <div id="app2"> <p># v-withͰͷnumberΛࢠͷcountͱͯ͠ࢦఆ </p> <div v-component="child" v-with="count: number"></div> </div> // JS var child = { template: '<span>{{$data|json}}</span>', data: function () { return { msg: 'hello' } } } var app1 = new Vue({ components: { child: child } }).$mount('#app1') var app2 = new Vue({ data: { number: 3 }, components: { child: child } }).$mount('#app2') http://jsfiddle.net/kazupon/pqw77weq/3/ -
v-with directive • ࢠͷ$dataΛյͯ͠͠·͏ ex2: $dataͷॳظΛม͑ͯ͠·͏ <!-- HTML --> <div
id="app1"> <p># v-withͳ͠</p> <div v-component="child"></div> </div> <div id="app2"> <p># v-withͰσʔλΛॻ͖͑ͯ͠·͏</p> <div v-component="child" v-with="msg: say"></div> </div> // JS var child = { template: '<span>{{$data|json}}</span>', data: function () { return { msg: 'hello' } } } var app1 = new Vue({ components: { child: child } }).$mount('#app1') var app2 = new Vue({ data: { say: 'world' }, components: { child: child } }).$mount('#app2') http://jsfiddle.net/kazupon/tceb6wLt/2/ -
paramsAttbites option • Φϓγϣϯ໊propsʹ มߋ • {{*prop}}ͰҰํ (one-way)ͳόΠϯ σΟϯάͱͯ͠໌ࣔతʹ ࢦఆ͕Ͱ͖Δ
https:// jsfiddle.net/ kazupon/cecypzan/3/ <!-- HTML --> <div id="app"> <div> <span>:</span> <input type="text" v-model="parentMsg"> </div> <div> <child child-msg="{{*parentMsg}}"></child> </div> </div> // JS new Vue({ el: '#app', data: { parentMsg: 'hello world !!' }, components: { child: { props: ['child-msg'], template: '<span>ࢠ:{{childMsg}}</span> <br><input type="text" v-model="childMsg">' } } }) -
paramsAttbites option • ෳͷmustacheΛɺprops optionͰఆٛͨ͠ϓϩύ ςΟʹࢦఆ͕Ͱ͖Δ <!-- HTML --> <div
id="app"> <child child-msg="{{a}} {{b}}"></child> </div> // JS new Vue({ el: '#app', data: { a: 1, b: 2 }, components: { child: { props: ['child-msg'], template: '<span>{{childMsg}}</span>' } } }) https://jsfiddle.net/24ch88n2/2/ -
paramsAttbites option • ϑΟϧλؚΊΔ͜ͱ͕Ͱ͖Δ <!-- HTML --> <div id="app"> <child
child-msg="{{msg | uppercase}}"></child> </div> // JS new Vue({ el: '#app', data: { msg: 'hello' }, components: { child: { props: ['child-msg'], template: '<span>{{childMsg}}</span>' } } }) https://jsfiddle.net/kazupon/ondpvuk8/1/ -
paramsAttbites option • expressionsΛؚΊΔ͜ͱͰ͖Δ https://jsfiddle.net/kazupon/pnszx8rc/1/ <!-- HTML --> <div id="app">
<child child-msg="{{a}} + {{b}} = {{a + b}}"></child> </div> // JS new Vue({ el: '#app', data: { a: 1, b: 2 }, components: { child: { props: ['child-msg'], template: '<span>{{childMsg}}</span>' } } }) -
paramsAttbites option • ͷϓϩύςΟ͕ઃఆՄೳͰͳ͍ͱ͖ɺϓϩύςΟ ࣗಈతʹҰํͳόΠϯσΟϯάʹͳΔ <!-- HTML --> <div id="app">
<child child-msg="{{a + b}}"></child> </div> // JS new Vue({ el: '#app', data: { a: 1, b: 2 }, components: { child: { props: ['child-msg'], template: '<span>{{childMsg}}</span>' } } }) -
v-component directive • v-component directiveഇࢭ • ࠓޙΧελϜλάͰί ϯϙʔωϯτΛར༻͢Δ (ϋΠϑϯ”-“ඞਢͰ ͳ͘ͳͬͨ)
<!-- HTML --> <div id="app"> <input type="text" v-model="message"> <view text="{{message}}"></view> </div> // JS new Vue({ el: '#app', data: { message: 'hello world !!' }, components: { view: { props: ['text'], template: '<span>{{text}}</span>' } } }) https://jsfiddle.net/kazupon/Lkqdd1y0/ -
v-component directive • ಈతͳίϯϙʔωϯτ<component is=“{{view}}”> <!-- HTML --> <div id="app">
<component is="{{view}}"></component> <input type="radio" name="view1" value="view1" v-model="view"> <input type="radio" name="view2" value="view2" v-model="view"> </div> // JS new Vue({ el: '#app', data: { view: ‘view1' }, components: { view1: { template: '<span>view1</span>' }, view2: { template: '<span>view2</span>' } } }) https://jsfiddle.net/kazupon/oev3bahL/2/