ãã®ã¬ã€ãã¯ããªãã®Vue.js ã®ã³ãŒããçµ±äžããæ¹æ³ãæäŸããŸãã
- éçºè ïŒããŒã ã¡ã³ããããåé¡ãç解ããèŠã€ããããããã
- IDEãããã³ãŒãã解éããæ¯æŽãæäŸããããããã
- ãã§ã«äœ¿çšããŠãããã«ãããŒã«ãïŒåïŒå©çšããããããã
- å¥ã ã®ã³ãŒãã®å¡ãèããäŸçµŠããããããã
ãã®ã¬ã€ãã¯De Voorhoedeã«ããRiotJS Style Guideã«åºæ¿ãåãäœãããŸããã
- ã³ã³ããŒãã³ãã®èšè¿°ãã·ã³ãã«ã«ä¿ã€
- ã³ã³ããŒãã³ãã®propsãããªããã£ãã«ä¿ã€
- ã³ã³ããŒãã³ãã®propsã®å©çš
this
ãcomponent
ã«å²ãåœãŠã- ã³ã³ããŒãã³ãæ§æ
- ã³ã³ããŒãã³ãã€ãã³ãå
this.$parent
ãé¿ããthis.$refs
ã¯æ³šæããŠäœ¿çšãã- ã¹ã¿ã€ã«ã¹ã³ãŒããšããŠã³ã³ããŒãã³ãåã䜿çšãã
- ã³ã³ããŒãã³ãAPIãããã¥ã¡ã³ãåãã
- ã³ã³ããŒãã³ããã¢ã®è¿œå
- ã³ã³ããŒãã³ããã¡ã€ã«ãLintãã
- å¿ èŠã«å¿ããŠã³ã³ããŒãã³ããäœæãã
åžžã«åäžã®æ©èœãæã€å°ããªã¢ãžã¥ãŒã«ããã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããŸãããã
ã¢ãžã¥ãŒã«ã¯ã¢ããªã±ãŒã·ã§ã³ã®èªå·±å®çµåã®å°ããªéšåã§ããç¹ã«Vue.jsã©ã€ãã©ãªã¯ããªããview-logicãªã¢ãžã¥ãŒã«ãäœããããã«èšèšãããŠããŸãã
å°ããªã¢ãžã¥ãŒã«ã«ããããšã§ãããªããšä»ã®éçºè äž¡æ¹ã«ãšã£ãŠãåŠã³ããããç解ãããããç¶æãããããåå©çšããããããããŠã ãããã°ãããããªããŸãã
åVueã³ã³ããŒãã³ãïŒã¢ãžã¥ãŒã«ã®ãããªãã®ïŒã¯ FIRST: ã²ãšã€ã®ããšã«éäžã (Focused (åäžè²¬ä»»))ãç¬ç«ããŠããŠ(Independent)ã åå©çšå¯èœã§(Reusable)ãå°ãã(Small) ãããŠãã¹ãå¯èœ (Testable)ã§ãªããã°ãªããŸããã
ããããªãã®ã³ã³ããŒãã³ããå€ãã®ããšãããŠããŠå€§ããããå Žåãã²ãšã€ã®ããšã ãããããããå°ããªã³ã³ããŒãã³ãã«åããŸãããã çµéšåããèšããšãåã³ã³ããŒãã³ãã¯ïŒïŒïŒè¡ä»¥äžã®ã³ãŒãã«ãªãããã«ããã®ãããã§ãããããŸããäŸãã°ã¹ã¿ã³ãã¢ããŒã³ã®ãã¢ãè¿œå ããããšã«ãã£ãŠã Vueã³ã³ããŒãã³ããç¬ç«ããŠåäœããããšã確èªããŸãããã
åã³ã³ããŒãã³ãã®ååã¯ã
- æå³ã®ããååã§: å ·äœçéãããæœè±¡çéããã
- çã: ïŒãŸãã¯ïŒèªã
- çºé³å¯èœ: ãããã«ã€ããŠè©±ããããã«ãããã
ã§ããã¹ãã§ãã
ããã«Vueã³ã³ããŒãã³ãã®ååã¯ã
- ã«ã¹ã¿ã èŠçŽ ä»æ§ã«æºæ : ãã€ãã³ãå«ã¿ã äºçŽèªã¯äœ¿çšããªãã
app-
ããŒã ã¹ããŒã¹: éåžžã«æ±çšçããããã¯ïŒèªã§ããã°ãä»ã®ãããžã§ã¯ãã§ã容æã«åå©çšã§ããã
ã§ããã¹ãã§ãã
- ååã¯ãã³ã³ããŒãã³ãã«ã€ããŠäŒè©±ãããšãã«äœ¿çšãããŸãããããã£ãŠãããã¯çããæå³ããããçºé³å¯èœã§ãªããã°ãªããŸããã
<!-- æšå¥š -->
<app-header></app-header>
<user-list></user-list>
<range-slider></range-slider>
<!-- éæšå¥š -->
<btn-group></btn-group> <!-- çãã§ãã, çºé³ãå°é£ã§ãã代ããã«`button-group`ã䜿ããŸããã -->
<ui-slider></ui-slider> <!-- å
šãŠã³ã³ããŒãã³ãã¯uiãªã®ã§ãäžèº«ãè¡šããŠããŸãã -->
<slider></slider> <!-- ã«ã¹ã¿ã èŠçŽ ä»æ§ã«æºæ ããŠããŸãã -->
Vue.jsã®ã€ã³ã©ã€ã³ã®èšè¿°ã¯100ïŒ JavaScriptã§ããããã¯éåžžã«åŒ·åãªããšã§ãããè€éã«ãªãå¯èœæ§ããããšããããšã§ãã ã§ãã®ã§ãã·ã³ãã«ãªèšè¿°ãä¿ã€ããã«ããŸãããã.
- è€éãªã€ã³ã©ã€ã³ã®èšè¿°ã¯å€èªå°é£ã§ãã
- ã€ã³ã©ã€ã³ã®èšè¿°ã¯ä»ã®å Žæã§åå©çšã§ããŸãããããã¯ã³ãŒãã®éè€ãšè æã«ã€ãªãããŸãã
- IDEã¯åºæ¬çã«åŒã®æ§æãµããŒãæ©èœãæã£ãŠããªããããèªåè£å®ãæ€èšŒãè¡ãããšãã§ããŸããã
ããããŸãã«ãè€éã ã£ãããå€èªå°é£ãªå Žåã¯ã¡ãœããããŸãã¯computedããããã£ã«ç§»åãããŸããã!
<!-- æšå¥š -->
<template>
<h1>
{{ `${year}-${month}` }}
</h1>
</template>
<script type="text/javascript">
export default {
computed: {
month() {
return this.twoDigits((new Date()).getUTCMonth() + 1);
},
year() {
return (new Date()).getUTCFullYear();
}
},
methods: {
twoDigits(num) {
return ('0' + num).slice(-2);
}
},
};
</script>
<!-- éæšå¥š -->
<template>
<h1>
{{ `${(new Date()).getUTCFullYear()}-${('0' + ((new Date()).getUTCMonth()+1)).slice(-2)}` }}
</h1>
</template>
Vue.jsã¯è€éãªJavaScriptãªããžã§ã¯ããæž¡ããããã«ãªã£ãŠããŸãã, ã³ã³ããŒãã³ãã®propsã¯ã§ããã ãããªããã£ãã«ä¿ã€ ããã«ããã¹ãã§ããè€éãªãªããžã§ã¯ãã®äœ¿çšãé¿ããJavaScriptããªããã£ããšé¢æ°ã®ã¿ã䜿ãããã«ããŸãããã
- åpropã®å±æ§ãå¥ã ã«äœ¿çšããããšã«ãããã³ã³ããŒãã³ãã¯æ確ã§è¡šçŸåè±ããªAPIãæã€ããšã«ãªããŸãã
- propsã®å€ãšããŠããªããã£ããšãã¡ã³ã¯ã·ã§ã³ã®ã¿ã䜿çšããããšã§ãã³ã³ããŒãã³ãã®APIããã€ãã£ãHTMLïŒ5ïŒã®APIã«äŒŒããã®ã«ã§ããŸãã
- åpropã®å±æ§ã䜿çšããããšã§ãä»ã®éçºè ãã³ã³ããŒãã³ãã€ã³ã¹ã¿ã³ã¹ã«äœãæž¡ãããããç解ãããããªããŸãã
- è€éãªãªããžã§ã¯ããæž¡ããããšããã®ãªããžã§ã¯ãã®ã©ã®ããããã£ãšã¡ãœãããå®éã«ã«ã¹ã¿ã ã³ã³ããŒãã³ãã§äœ¿ããããããããã«ãããªããŸããããã«ããã³ãŒãã®ãªãã¡ã¯ã¿ãªã³ã°ãé£ãããªããè æãæãããšã«ãªããŸãã
ããªããã£ããŸãã¯é¢æ°ãå€ãšããpropsããšã®ã³ã³ããŒãã³ãå±æ§ã䜿çšããŸãã
<!-- æšå¥š -->
<range-slider
:values="[10, 20]"
min="0"
max="100"
step="5"
:on-slide="updateInputs"
:on-end="updateResults">
</range-slider>
<!-- éæšå¥š -->
<range-slider :config="complexConfigObject"></range-slider>
Vue.jsã§ã¯ãããªãã®ã³ã³ããŒãã³ãã®propsã¯ããªãã®APIã§ããé äžã§äºæž¬ããããAPIã¯ãä»ã®éçºè ãããªãã®ã³ã³ããŒãã³ãã䜿çšããã®ãç°¡åã«ããŸãã
ã³ã³ããŒãã³ãã®propsã¯ã«ã¹ã¿ã HTMLå±æ§ãä»ããŠæž¡ãããŸãã ãããã®å±æ§ã®å€ã¯Vue.jsãã¬ãŒã³ã¹ããªã³ã° (:attr="å€"
ãŸã㯠v-bind:attr="å€"
)ãããŸãã¯ç¡ãããšããããŸãã ããªã㯠ã³ã³ããŒãã³ãã®propsãå©çš ããŠããããã®ç°ãªãã±ãŒã¹ã«å¯Ÿå¿ã§ããããã«ããŸãããã
ã³ã³ããŒãã³ãã®propsãå©çšããããšã§ãããªãã®ã³ã³ããŒãã³ããåžžã«æ©èœããããã«ãªããŸãïŒé²åŸ¡çããã°ã©ãã³ã°ïŒãããã¯åŸã§ä»ã®éçºè ããããªããæ³å®ããŠããªãæ¹æ³ã§ã³ã³ããŒãã³ãã䜿çšããå Žåã§ãã§ãã
- propsã®ããã©ã«ãå€ã䜿çšããŸãã
- æåŸ
ããã¿ã€ãã®å€ã®æ€èšŒã®ããã«ã
type
ãªãã·ã§ã³ã䜿çšããŸãã[1*] - 䜿çšãããåã«propsãååšããããã§ãã¯ããŸãã
<template>
<input type="range" v-model="value" :max="max" :min="min">
</template>
<script type="text/javascript">
export default {
props: {
max: {
type: Number, // [1*] ããã¯'max'propãæ°å€ã§ããããšãæ€èšŒããŸãã
default() { return 10; },
},
min: {
type: Number,
default() { return 0; },
},
value: {
type: Number,
default() { return 4; },
},
},
};
</script>
Vue.jsã³ã³ããŒãã³ãã®ã³ã³ããã¹ãå
ã§ã¯ã this
ã¯ã³ã³ããŒãã³ãã€ã³ã¹ã¿ã³ã¹ã«ãã€ã³ããããŠããŸãã
ãããã£ãŠãå¥ã®ã³ã³ããã¹ãã§åç
§ããå¿
èŠãããå Žåã¯ã this
ãcomponent
ãšããŠäœ¿çšã§ããããšã確èªããŠãã ããã
èšãæããã°: const self = this;
ã®ãããªã³ãŒãã£ã³ã°ã¯ãã ããªã㧠ãã ããã Vueã³ã³ããŒãã³ãã®äœ¿çšã¯å®å
šã§ãã
- ES6ã䜿ã£ãŠããå Žåã
this
ãå€æ°ã«ä¿åããŠããå¿ èŠã¯ãããŸããã - éåžžãã¢ããŒé¢æ°ã䜿çšããã°éçã¹ã³ãŒãã¯ä¿æãããŸãã
- ES6ã䜿çšããŠããªãããã«ã
ã¢ããŒé¢æ°
ã䜿çšã§ããªãå Žåã¯ãthis
ãå€æ°ã«æ ŒçŽããå¿ èŠããããŸãããããå¯äžã®äŸå€ã§ãã
<script type="text/javascript">
export default {
methods: {
hello() {
return 'hello';
},
printHello() {
console.log(this.hello());
},
},
};
</script>
<!-- éæšå¥š -->
<script type="text/javascript">
export default {
methods: {
hello() {
return 'hello';
},
printHello() {
const self = this; // äžèŠ
console.log(self.hello());
},
},
};
</script>
è«ççã«èãããããæèã®æµãã«åŸããããããã«ããŸããã. æ¹æ³ãèŠãŠãã ããã
- ã³ã³ããŒãã³ããæ確ãã€ã°ã«ãŒãåããããªããžã§ã¯ããšããããšã§ãã³ãŒããèªã¿ãããããéçºè ã¯ã³ãŒãã®åºæºãç°¡åã«æãŠãããã«ãªããŸãã
- propsãdataãcomputedãwatchesããããŠmethodsãã¢ã«ãã¡ãããé ã«äžŠã¹ãããšã§ãèŠã€ãããããªããŸãã
- ç¹°ãè¿ãã«ãªããŸãã, ã³ã³ããŒãã³ããã°ã«ãŒãåããããšã§èªã¿ããããªããŸã (nameãextendsãpropsãdataãããŠcomputedãcomponentsã watchãmethodsãlifecycle methodsããªã©);
name
å±æ§ã䜿ããŸããã. vue devtoolsãšnameå±æ§ã䜿ããšãéçº/ãã¹ãã容æã«ãªããŸãã- BEMããŸã㯠rscssã®ãããªCSSã®åœåæ¹æ³è«ã䜿ããŸãããã - 詳现?;
- Vue.jsã®è£œäœè Evan Youãæšå¥šããããã«ããã³ãã¬ãŒãã¹ã¯ãªãã圢åŒã®.vueãã¡ã€ã«æ§æã䜿çšããŸãããã
ã³ã³ããŒãã³ã®ãæ§æ:
<template lang="html">
<div class="Ranger__Wrapper">
<!-- ... -->
</div>
</template>
<script type="text/javascript">
export default {
// ãã®ã¡ãããªèŠçŽ ãå¿ããªãã§ãã ãã
name: 'RangeSlider',
// æ°ããã³ã³ããŒãã³ããåæããŸã
extends: {},
// ã³ã³ããŒãã³ãã®ããããã£/å€
props: {
bar: {}, // ã¢ã«ãã¡ãããé
foo: {},
fooBar: {},
},
// å€æ°
data() {},
computed: {},
// ä»ã®ã³ã³ããŒãã³ãã䜿çš
components: {},
// ã¡ãœãã
watch: {},
methods: {},
// ã³ã³ããŒãã³ãã©ã€ããµã€ã¯ã«ããã¯
beforeCreate() {},
mounted() {},
};
</script>
<style scoped>
.Ranger__Wrapper { /* ... */ }
</style>
Vue.jsã¯ãã¹ãŠã®Vueãã³ãã©é¢æ°ãæäŸããåŒã¯ViewModelã«å³å¯ã«ãã€ã³ããããŠããŸããåã³ã³ããŒãã³ãã®ã€ãã³ãã¯ãéçºäžã®åé¡ãåé¿ãããããªè¯ãååä»ãã®ã¹ã¿ã€ã«ãé©çšããŸãããã 以äžã® çç± ãåç §ããŠãã ããã
- éçºè ã¯å¥œããªã€ãã³ãåãèªç±ã«äœ¿ãããšãã§ãããããæ··ä¹±ãæãå¯èœæ§ããããŸãã
- ã€ãã³ãã«ååãã€ããèªç±ã¯ã DOMãã³ãã¬ãŒãã®éäºææ§ã«ã€ãªããå¯èœæ§ããããŸãã
- ã€ãã³ãååã¯ã±ããã±ãŒã¹ã«ããã¹ãã§ãã
- ããªãã®ã³ã³ããŒãã³ãã§å€éšã§é¢å¿ãæã€ç¬èªã®ã¢ã¯ã·ã§ã³ã®ãããupload-successãupload-errorãããã«ã¯dropzone-upload-successã dropzone-upload-errorã®ãããªåºæã®ååãã€ããå¿ èŠããããŸãã(ã¹ã³ãŒãä»ããã¬ãã£ãã¯ã¹ã䜿çšããå¿ èŠãããå Žåã¯)
- ã€ãã³ãåã¯æ«å°Ÿãäžå®åœ¢ã®åè©(äŸ client-api-load)ããŸãã¯åè©(äŸ drive-upload-success)ã§çµããã¹ãã§ãã(åºå ž);
Vue.jsã¯ã芪ã³ã³ããã¹ãã«ã¢ã¯ã»ã¹ã§ãããã¹ããããã³ã³ããŒãã³ãããµããŒãããŠããŸãã ããªãã®Vueã³ã³ããŒãã³ãã®å€éšã®ã³ã³ããã¹ãã«ã¢ã¯ã»ã¹ããããšã¯ã ã¢ãžã¥ãŒã«ããŒã¹ã®éçºã®FIRSTã®ã«ãŒã«ã«éåããããã ** this.$parent
ã®äœ¿çšãé¿ãã ** ã¹ãã§ãã
- vueã³ã³ããŒãã³ãã¯ãä»ã®ã³ã³ããŒãã³ããšåæ§ã«ãç¬ç«ããŠåäœããå¿ èŠããããŸãã ã³ã³ããŒãã³ãããã®èŠªã«ã¢ã¯ã»ã¹ããå¿ èŠãããå Žåããã®ã«ãŒã«ã¯å£ãããŸãã
- ã³ã³ããŒãã³ãããã®èŠªã«ã¢ã¯ã»ã¹ããå¿ èŠãããå Žåãå¥ã®ã³ã³ããã¹ãã§åå©çšããããšã¯ã§ããŸããã
- å±æ§/ããããã£ã䜿çšããŠã芪ããåã³ã³ããŒãã³ãã«å€ãæž¡ããŸãã
- å±æ§ã®åŒã®ã³ãŒã«ããã¯ã䜿çšããŠã芪ã³ã³ããŒãã³ãã§å®çŸ©ãããã¡ãœãããåã³ã³ããŒãã³ãã«æž¡ããŸãã
- åã³ã³ããŒãã³ãããã€ãã³ããçºè¡ããããã芪ã³ã³ããŒãã³ãã§ãã£ããããŸãã
Vue.jsã¯ref
å±æ§ãä»ããŠä»ã®ã³ã³ããŒãã³ãããåºæ¬çãªHTMLèŠçŽ ã®ã³ã³ããã¹ãã«ã¢ã¯ã»ã¹ã§ããã³ã³ããŒãã³ãããµããŒãããŠããŸãããã®å±æ§ã¯this.$refs
ãä»ããŠãã³ã³ããŒãã³ããŸãDOMèŠçŽ ã®ã³ã³ããã¹ãã«ã¢ã¯ã»ã¹å¯èœãªæ¹æ³ãæäŸããŸããã»ãšãã©ã®å Žåã this.$refs
ãä»ã㊠ä»ã®ã³ã³ããŒãã³ã ã®ã³ã³ããã¹ãã«ã¢ã¯ã»ã¹ããå¿
èŠæ§ã¯é¿ããããšãã§ããŸãããã®ãããééã£ãã³ã³ããŒãã³ãAPIãé¿ããããã«ããã䜿çšãããšãã¯æ³šæãå¿
èŠã§ãã
- vueã³ã³ããŒãã³ãã¯ãä»ã®ã³ã³ããŒãã³ããšåæ§ã«ãç¬ç«ããŠåäœããå¿ èŠããããŸãã ã³ã³ããŒãã³ããå¿ èŠãªãã¹ãŠã®ã¢ã¯ã»ã¹ããµããŒãããŠããªãå Žåãããã¯æªãèšèš/å®è£ ã§ãã
- ã»ãšãã©ã®ã³ã³ããŒãã³ãã§ã¯ãããããã£ãšã€ãã³ãã§ååã§ãã
- è¯ãã³ã³ããŒãã³ãAPIãäœããŸãããã
- ç¬åµçãªã³ã³ããŒãã³ãã®ç®çã«åžžã«æ³šæããŠãã ããã
- ç¹æ®ãªã³ãŒããæžããªãã§ãã ããããžã§ããªãã¯ã³ã³ããŒãã³ãå ã«ç¹æ®ãªã³ãŒããèšè¿°ããå¿ èŠãããå Žåã¯ãAPIãååã«äžè¬çã§ãªãããä»ã®ã±ãŒã¹ã管çããããã«æ°ããã³ã³ããŒãã³ããå¿ èŠã«ãªããšããããšã§ãã
- ãã¹ãŠã®propsããã§ãã¯ããŠãæ¬ ããŠãããã®ããããã©ãã確èªããŸãã ããããã°ã課é¡ãäœæãããããªãã®ã³ã³ããŒãã³ãã匷åããŸãããã
- ãã¹ãŠã®ã€ãã³ãããã§ãã¯ããŸããããã»ãšãã©ã®ã±ãŒã¹ã§ãéçºè ã¯å-芪ã®ã³ãã¥ãã±ãŒã·ã§ã³ïŒã€ãã³ãïŒãå¿ èŠã§ããããšãå¿ããŠããŸããŸãã ãã®ãããïŒpropsã䜿çšããïŒèŠª-åã®ã³ãã¥ãã±ãŒã·ã§ã³ã ããèŠããŠããã®ã§ãã
- Propsã¯äžãžãeventsã¯äžãž! ãŽãŒã«ãšããŠè¯ãAPIãšåé¢ãèŠæ±ãããå Žåãã³ã³ããŒãã³ããã¢ããã°ã¬ãŒãããŸãããã
- propsãeventsãæ¶èããã³ã³ããŒãã³ãã§ã®
this.$refs
ã®å©çšãå©ã«ããªã£ãŠãããšãã¯ãããã䜿çšããã¹ãã§ãã (äžã®äŸãåç §ããŠãã ãã). - ããŒã¿ãã€ã³ãã£ã³ã°ããã£ã¬ã¯ãã£ãã§èŠçŽ ãæäœã§ããªãå Žåã (
jQuery
,document.getElement*
,document.queryElement
ã®ä»£ããã«)this.$refs
ã䜿çšããŠDOMèŠçŽ ã«ã¢ã¯ã»ã¹ããããšã¯è¯ãæ¹æ³ã§ãã
<!-- è¯ãã§ããrefã¯å¿
èŠãããŸããã -->
<range :max="max"
:min="min"
@current-value="currentValue"
:step="1"></range>
<!-- this.$refsã䜿çšããå Žåã®è¯ãäŸã§ãã -->
<modal ref="basicModal">
<h4>Basic Modal</h4>
<button class="primary" @click="$refs.basicModal.hide()">Close</button>
</modal>
<button @click="$refs.basicModal.open()">Open modal</button>
<!-- ã¢ãŒãã«ã³ã³ããŒãã³ã -->
<template>
<div v-show="active">
<!-- ... -->
</div>
</template>
<script>
export default {
// ...
data() {
return {
active: false,
};
},
methods: {
open() {
this.active = true;
},
hide() {
this.active = false;
},
},
// ...
};
</script>
<!-- çºè¡ãããå¯èœæ§ã®ãããã®ãžã®ã¢ã¯ã»ã¹ã¯é¿ããŸããã -->
<template>
<range :max="max"
:min="min"
ref="range"
:step="1"></range>
</template>
<script>
export default {
// ...
methods: {
getRangeCurrentValue() {
return this.$refs.range.currentValue;
},
},
// ...
};
</script>
Vue.jsã®ã³ã³ããŒãã³ãèŠçŽ ã¯ãã¹ã¿ã€ã«ã¹ã³ãŒãã®ã«ãŒããšããŠéåžžã«ãã䜿çšãããã«ã¹ã¿ã èŠçŽ ã§ãã ãããã¯ãã³ã³ããŒãã³ãåã¯CSSã¯ã©ã¹ããŒã ã¹ããŒã¹ãšããŠã䜿çšã§ããŸãã
-
ã³ã³ããŒãã³ãèŠçŽ ã®ã¹ã¿ã€ã«ãã¹ã³ãŒããããšãã³ã³ããŒãã³ãã®å€ã«ã¹ã¿ã€ã«ãæŒããã®ãé²ããããäºæž¬å¯èœæ§ãåäžããŸãã
-
ã¢ãžã¥ãŒã«ãã£ã¬ã¯ããªã«åãååã䜿çšãããšãVue.jsã³ã³ããŒãã³ããšã¹ã¿ã€ã«ã«ãŒããåããšããã«å±ããŠããããšãéçºè ãç解ãããããªããŸãã
BEMãšOOCSSã«åºã¥ãããŒã ã¹ããŒã¹ãã¬ãã£ãã¯ã¹ãšããŠã³ã³ããŒãã³ãåã䜿ããŸãããã ãã㊠ã¹ã¿ã€ã«ã¯ã©ã¹ã§scoped
å±æ§ã䜿ããŸãããã
scoped
ã䜿çšãããšã<style>
ã«ãããã¹ãŠã®ã¯ã©ã¹ã«ã·ã°ããã£ãè¿œå ããããVueã³ã³ãã€ã©ã«æ瀺ããŸãããã®ã·ã°ããã£ã¯ãã³ã³ããŒãã³ããæ§æãããã¹ãŠã®ã¿ã°ã«ã³ã³ããŒãã³ãCSSãé©çšãããããã©ãŠã¶ã«ïŒãµããŒãããŠããå ŽåïŒåŒ·å¶ããCSSã®ã¹ã¿ã€ãªã³ã°ãæŒããªãããã«ããŸãã
<style scoped>
/* æšå¥š */
.MyExample { }
.MyExample li { }
.MyExample__item { }
/* éæšå¥š */
.My-Example { } /* ã³ã³ããŒãã³ãåãŸãã¯ã¢ãžã¥ãŒã«åã«ã¹ã³ãŒããããŠããããBEMæºæ ã§ããããŸãã */
</style>
Vue.jsã³ã³ããŒãã³ãã®ã€ã³ã¹ã¿ã³ã¹ã¯ãã¢ããªã±ãŒã·ã§ã³å
ã®ã³ã³ããŒãã³ãèŠçŽ ã䜿çšããŠäœæãããŸããã€ã³ã¹ã¿ã³ã¹ã¯ãã®ã«ã¹ã¿ã å±æ§ã«ãã£ãŠæ§æãããŸããä»ã®éçºè
ãã³ã³ããŒãã³ãã䜿çšãããšãã®ããããããã®ã«ã¹ã¿ã å±æ§ïŒã³ã³ããŒãã³ãã®APIïŒãREADME.md
ãã¡ã€ã«ã«èšè¿°ããŸãããã
- ããã¥ã¡ã³ããŒã·ã§ã³ã¯ãéçºè ã«ããã®ã³ãŒãã®ãã¹ãŠãäŒããããšãªããã³ã³ããŒãã³ãã®æŠèŠãé«ã¬ãã«ã§è¡šç€ºããŸããããã«ãããã³ã³ããŒãã³ãã®ã¢ã¯ã»ã¹ã容æã«ãªãã䜿ãããããªããŸãã
- ã³ã³ããŒãã³ãã®APIã¯ããããæ§æããã«ã¹ã¿ã å±æ§ã®ã»ããã§ãããããã£ãŠããããã¯ãã³ã³ããŒãã³ãã䜿çšããïŒéçºããªãïŒéçºè ã«ãšã£ãŠç¹ã«éèŠã§ãã
- ããã¥ã¡ã³ããŒã·ã§ã³ã¯APIã圢åŒåããã³ã³ããŒãã³ãã®ã³ãŒããå€æŽãããšãã«äžäœäºææ§ãç¶æããæ©èœãéçºè ã«æããŸãã
README.md
ã¯æåã«èªã¿èŸŒãŸããããã¥ã¡ã³ãã®ããã¡ã¯ãã¹ã¿ã³ããŒããã¡ã€ã«åã§ãã ã³ãŒããªããžããªãã¹ãã£ã³ã°ãµãŒãã¹ïŒGithubãBitbucketãGitlabãªã©ïŒã¯ããœãŒã¹ãã£ã¬ã¯ããªãåç §ãããšãã«ãREADMEãã¡ã€ã«ã®å 容ãçŽæ¥è¡šç€ºããŸããããã¯ç§ãã¡ã®ã¢ãžã¥ãŒã«ãã£ã¬ã¯ããªã«ãåœãŠã¯ãŸããŸãã
ã³ã³ããŒãã³ãã®ã¢ãžã¥ãŒã«ãã£ã¬ã¯ããªã«README.md
ãã¡ã€ã«ãè¿œå ããŸãããã
range-slider/
âââ range-slider.vue
âââ range-slider.less
âââ README.md
READMEãã¡ã€ã«ã«ãã¢ãžã¥ãŒã«ã®æ©èœãšäœ¿çšæ¹æ³ãèšè¿°ããŸãããã vueã³ã³ããŒãã³ãã®å ŽåãAPIãšããŠãµããŒããããŠããã«ã¹ã¿ã å±æ§ãèšè¿°ããã®ãæã圹ç«ã¡ãŸãã
range sliderã¯ãã¹ã©ã€ããŒã¬ãŒã«ã®ãã³ãã«ãéå§å€ãšçµäºå€ã®äž¡æ¹ã§ãã©ãã°ããŠæ°å€ç¯å²ãèšå®ã§ããŸãã
ãã®ã¢ãžã¥ãŒã«ã¯ã¯ãã¹ãã©ãŠã¶ãšã¿ãããµããŒãã®ãããnoUiSliderã䜿çšããŸãã
<range-slider>
次ã®ã«ã¹ã¿ã ã³ã³ããŒãã³ãå±æ§ããµããŒãããŠããŸã:
å±æ§ | å | 説æ |
---|---|---|
min |
æ°å€ | ã¬ã³ãžã®å§ãŸãã®æ°å€ (äžé)ã |
max |
æ°å€ | ã¬ã³ãžã®çµããã®æ°å€ (äžé). |
values |
æ°å€[] ä»»æ | éå§å€ãšçµäºå€ãå«ãé
åã äŸ. values="[10, 20]" . ããã©ã«ãã¯[opts.min, opts.max] . |
step |
æ°å€ ä»»æ | ã€ã³ã¯ãªã¡ã³ã/ãã¯ãªã¡ã³ãã®æ°å€ãããã©ã«ãã¯1ã |
on-slide |
é¢æ° ä»»æ | ãŠãŒã¶ãŒãéå§ïŒHANDLE == 0 ïŒãŸãã¯çµäºïŒHANDLE == 1 ïŒãã³ãã«ããã©ãã°ããŠãããšãã«ã(values, HANDLE) ã§åŒã³åºãããé¢æ°ãäŸ. on-slide={ updateInputs } component.updateInputs = (values, HANDLE) => { const value = values[HANDLE]; } . |
on-end |
é¢æ° ä»»æ | ãŠãŒã¶ãŒããã³ãã«ã®ãã©ãã°ãåæ¢ãããšãã(values, HANDLE) ã§åŒã³åºãããé¢æ°ã |
ã¹ã©ã€ãã®å€èŠ³ãã«ã¹ã¿ãã€ãºããã«ã¯noUiSliderããã¥ã¡ã³ãã®Stylingã»ã¯ã·ã§ã³ãåç §ããŠãã ããã
æ§æã®ç°ãªãã³ã³ããŒãã³ãã®ãã¢ãå«ãindex.htmlãã¡ã€ã«ãè¿œå ããã³ã³ããŒãã³ãã®äœ¿çšæ¹æ³ã瀺ããŸãããã
- ã³ã³ããŒãã³ãã®ãã¢ã¯ãã³ã³ããŒãã³ããåç¬ã§åäœããããšã瀺ããŸãã
- ã³ã³ããŒãã³ãã®ãã¢ã¯ãããã¥ã¡ã³ããŒã·ã§ã³ãã³ãŒããæãèµ·ããåã«ãéçºè ã«ãã¬ãã¥ãŒãæäŸããŸãã
- ãã¢ã§ã¯ãã³ã³ããŒãã³ãã䜿çšã§ãããã¹ãŠã®å¯èœãªæ§æãšããªãšãŒã·ã§ã³ã瀺ãããšãã§ããŸãã
Lintersã¯ã³ãŒãã®äžè²«æ§ãé«ããæ§æãšã©ãŒã®è¿œè·¡ã«åœ¹ç«ã¡ãŸãã.vueãã¡ã€ã«ã¯ãããžã§ã¯ãã«eslint-plugin-html
ãè¿œå ããŠlintããããšãã§ããŸããvue-cliã䜿çšããã°ãESLintãããã©ã«ãã§æå¹ã«ããŠãããžã§ã¯ããéå§ã§ããŸãã
- ã³ã³ããŒãã³ããã¡ã€ã«ã®Lintã¯ããã¹ãŠã®éçºè ãåãã³ãŒãã¹ã¿ã€ã«ã䜿çšããããã«ããŸãã
- ã³ã³ããŒãã³ããã¡ã€ã«ã®Lintã¯ãé ããªãåã«æ§æãšã©ãŒã®è¿œè·¡ãããã®ã«åœ¹ç«ã¡ãŸãã
lintersãããªãã®*.vue
ãã¡ã€ã«ããã¹ã¯ãªãããæœåºããããã«ã¯, ã¹ã¯ãªããã<script>
ã³ã³ããŒãã³ãã®äžã«çœ®ãã ã³ã³ããŒãã³ãã®èšè¿°ãã·ã³ãã«ã«ä¿ã€ããã«ããŠãã ãã (lintersã¯ããããç解ã§ããªãã®ã§)ã ã°ããŒãã«å€æ°vue
ãšã³ã³ããŒãã³ãã®props
ãèš±å¯ããããã«ãªã³ã¿ãŒãèšå®ããŸãã
ESLintã«ã¯ãã³ã³ããŒãã³ããã¡ã€ã«ããã¹ã¯ãªãããæœåºããããã®è¿œå ã®ESLint HTML pluginãå¿ èŠã§ãã
ESLintã.eslintrc
ãã¡ã€ã«ã«èšå®ããŸãã(IDEãããã解éããããšãã§ããŸã):
{
"extends": "eslint:recommended",
"plugins": ["html"],
"env": {
"browser": true
},
"globals": {
"opts": true,
"vue": true
}
}
ESLintå®è¡
eslint src/**/*.vue
JSHint㯠(--extra-ext
ã䜿çšããŠ)HTMLãããŒã¹ã§ãã(--extract=auto
ã䜿çšããŠ)ã¹ã¯ãªãããæœåºã§ããŸãã
JSHintã.jshintrc
ãã¡ã€ã«ã«èšå®ããŸãã (IDEãããã解éããããšãã§ããŸã):
{
"browser": true,
"predef": ["opts", "vue"]
}
JSHintå®è¡
jshint --config modules/.jshintrc --extra-ext=html --extract=auto modules/
泚æ: JSHintã¯vue
ãæ¡åŒµåãšããŠåãå
¥ãã, htmlã ããåãå
¥ããŸãã
Vue.jsã¯ã³ã³ããŒãã³ããã¬ãŒã ã¯ãŒã¯ã«åºã¥ããŠããŸããã³ã³ããŒãã³ãã®äœæã¿ã€ãã³ã°ãç¥ããªããšã次ã®ãããªåé¡ãçºçããå¯èœæ§ããããŸãã
- ã³ã³ããŒãã³ãã倧ããããå Žåã¯ãããããïŒåïŒäœ¿çšããç¶æããããšã¯å°é£ã§ãã
- ã³ã³ããŒãã³ããå°ãããããšããããžã§ã¯ãã浞氎ããã³ã³ããŒãã³ãéã®éä¿¡ãå°é£ã«ãªããŸãã
- ããªãã®ãããžã§ã¯ãã®ããŒãºã«åãããŠã³ã³ããŒãã³ããæ§ç¯ããããšãåžžã«å¿ããªãã§ãã ããããããããããã®ã³ã³ããŒãã³ããåãåºãããšèããããã«ããŠãã ãããã©ã€ãã©ãªã®ããã«ãããžã§ã¯ãããåãåºããã°ãããå ç¢ã§äžè²«æ§ã®ãããã®ã«ãªããŸãã
- æ¢åã®ã³ã³ããŒãã³ããå®å®ããã³ã³ããŒãã³ãã«ã³ãã¥ãã±ãŒã·ã§ã³ïŒpropsãeventsïŒãçµã¿èŸŒãããšãã§ãããããã§ããã ãæ©ãã³ã³ããŒãã³ããæ§ç¯ããæ¹ãè¯ãã§ãããã
- ãŸããã§ããã ãæ©ãã¢ãŒãã«ãããããªãŒããŒãããŒã«ããŒãã¡ãã¥ãŒãããããŒãªã©ã®æçœãªã³ã³ããŒãã³ããäœæããŠã¿ãŠãã ãããããªãã®çŸåšã®ããŒãžããŸãã¯å šäœçã«ãããªããåŸã§å¿ èŠãšãªãããšãããªããç¥ã£ãŠããã³ã³ããŒãã³ããã
- 第ïŒã«ãæ°ããéçºããšã«ãããŒãžå šäœãŸãã¯ãã®äžéšåã«ã€ããŠãæ¥ãã§éçºããåã«èããŠã¿ãŠãã ããããã®äžéšãã³ã³ããŒãã³ãã§ããããšãåãã£ãŠããå Žåã¯ãäœæããŠãã ããã
- æåŸã«ããã確信ããªããã°ãã³ã³ããŒãã³ããäœããªãã§ãã ããïŒ"åŸã§åœ¹ç«ã€ãããããªã"ã³ã³ããŒãã³ãã§ããªãã®ãããžã§ã¯ããæ±æããã®ãé¿ããŸãããããããã¯ç©ºã£ãœã§ãæ°žé ã«ããã«ããã ããããããŸããããããžã§ã¯ãã®æ®ãã®éšåãšã®äºææ§ã®è€éããé¿ããããã«ããã®ç¶æ ã«ãªã£ãŠããããšã«æ°ã¥ãããããã«ãããå解ãããã泚æããŠãã ããã
ãã©ãŒã¯ããŠãã«ãªã¯ãšã¹ããéãããIssueãäœã£ãŠãã ããã
- ãã©ãžã«ãã«ãã¬ã«èª: Pablo Henrique Silva github:pablohpsilva, twitter: @PabloHPSilva
- ãã·ã¢èª: Mikhail Kuznetcov github:shershen08, twitter: @legkoletat