ã¬ã¤ã
åºæ¬çãªä½¿ãæ¹
- ã¤ã³ã¹ãã¼ã«
- ã¯ããã«
- Vue ã¤ã³ã¹ã¿ã³ã¹
- ãã³ãã¬ã¼ãæ§æ
- ç®åºããããã£ã¨ã¦ã©ããã£
- ã¯ã©ã¹ã¨ã¹ã¿ã¤ã«ã®ãã¤ã³ãã£ã³ã°
- æ¡ä»¶ä»ãã¬ã³ããªã³ã°
- ãªã¹ãã¬ã³ããªã³ã°
- ã¤ãã³ããã³ããªã³ã°
- ãã©ã¼ã å ¥åãã¤ã³ãã£ã³ã°
- ã³ã³ãã¼ãã³ãã®åºæ¬
ã³ã³ãã¼ãã³ãã®è©³ç´°
- ã³ã³ãã¼ãã³ãã®ç»é²
- ããããã£
- ã«ã¹ã¿ã ã¤ãã³ã
- ã¹ããã
- åç & éåæã³ã³ãã¼ãã³ã
- ç¹å¥ãªåé¡ã«å¯¾å¦ãã
ãã©ã³ã¸ã·ã§ã³ã¨ã¢ãã¡ã¼ã·ã§ã³
- Enter/Leave ã¨ãã©ã³ã¸ã·ã§ã³ä¸è¦§
- ç¶æ ã®ãã©ã³ã¸ã·ã§ã³
åå©ç¨ã¨æ§æ
- ããã¯ã¹ã¤ã³
- ã«ã¹ã¿ã ãã£ã¬ã¯ãã£ã
- æç»é¢æ°ã¨JSX
- ãã©ã°ã¤ã³
- ãã£ã«ã¿ã¼
ãã¼ã«
- åä¸ãã¡ã¤ã«ã³ã³ãã¼ãã³ã
- ãã¹ã
- TypeScript ã®ãµãã¼ã
- ãããã¯ã·ã§ã³ç°å¢ã¸ã®é ä¿¡
ã¹ã±ã¼ã«ã¢ãã
- ã«ã¼ãã£ã³ã°
- ç¶æ 管ç
- ãµã¼ããµã¤ãã¬ã³ããªã³ã°
- ã»ãã¥ãªãã£
å é¨
- ãªã¢ã¯ãã£ãã®æ¢æ±
移è¡
- Vue 1.x ããã®ç§»è¡
- Vue Router 0.7.x ããã®ç§»è¡
- Vuex 0.6.x ãã 1.0 ã¸ã®ç§»è¡
ãã®ä»
- ä»ã®ãã¬ã¼ã ã¯ã¼ã¯ã¨ã®æ¯è¼
- Vue.js ã³ãã¥ããã£ã¸åå ãã¾ããã!
- ãã¼ã ã«ä¼ãã
v2.x 以åã®ããã¥ã¡ã³ãã§ãã v3.x ã®ããã¥ã¡ã³ããè¦ããå ´åã¯ãã¡ã
TypeScript ã®ãµãã¼ã
æçµæ´æ°æ¥: 2020å¹´11æ7æ¥
Vue CLI ã¯ãTypeScript ãã¼ã«ã®ãµãã¼ããçµã¿è¾¼ã¿ã§æä¾ãã¾ãã
NPM ããã±ã¼ã¸å ã®å ¬å¼å宣è¨
éçåã·ã¹ãã ã¯ãç¹ã«ã¢ããªã±ã¼ã·ã§ã³ãæé·ããã«ä¼´ããå¤ãã®æ½å¨çãªã©ã³ã¿ã¤ã ã¨ã©ã¼ãé²æ¢ããã®ã«å½¹ç«ã¡ã¾ãããã®ãããVue 㯠TypeScript åãã«å ¬å¼å宣è¨ãæä¾ãã¦ãããVue ã³ã¢ã ãã§ãªã Vue Router 㨠Vuex ãåæ§ã«æä¾ãã¦ãã¾ãã
ããã㯠NPM ã«å ¬éããã¦ãããããã¦ææ°ã® TypeScript 㯠NPM ããã±ã¼ã¸å ã®å宣è¨ã解決ããæ¹æ³ãç¥ã£ã¦ãã¾ããã¤ã¾ããNPM ã§ã¤ã³ã¹ãã¼ã«ããæãTypeScript ã Vue ã¨å ±ã«ä½¿ãããã®è¿½å ã®ãã¼ã«ãå¿ è¦ã¨ãã¾ããã
æ¨å¥¨æ§æ
// tsconfig.json
{
"compilerOptions": {
// ãã㯠Vue ã®ãã©ã¦ã¶ãµãã¼ãã«åããã¾ã
"target": "es5",
// ãã㯠`this` ã«ããããã¼ã¿ããããã£ã«å¯¾ãã¦å³å¯ãªæ¨è«ãå¯è½ã«ãã¾ã
"strict": true,
// webpack 2 以é ã¾ã㯠rollup ã使ç¨ãã¦ããå ´åãããªã¼ã·ã§ã¤ãã³ã°ãæ´»ç¨ããããã«
"module": "es2015",
"moduleResolution": "node"
}
}
ã³ã³ãã¼ãã³ãã¡ã½ããå
㧠this
ã®åããã§ãã¯ããã«ã¯ strict: true
(ãããã¯æä½ã§ã strict
ãã©ã°ã®ä¸é¨ã® noImplicitThis: true
) ãå«ããå¿
è¦ããããã¨ã«æ³¨æãã¦ãã ããã
ãã詳細ãªãã¨ã«ã¤ãã¦ã¯ TypeScript compiler options docs ãè¦ã¦ãã ããã
éçºãã¼ã«
ããã¸ã§ã¯ãä½æ
Vue CLI 3 㯠TypeScript ãå©ç¨ããæ°è¦ã®ããã¸ã§ã¯ããçæããäºãã§ãã¾ãã次ã®æé ã§éå§ãã¦ãã ãã:
# 1. ã¤ã³ã¹ãã¼ã«ããã¦ããªãå ´åã Vue CLI ãã¤ã³ã¹ãã¼ã«ãã¦ãã ãã
npm install --global @vue/cli
# 2. æ°è¦ã®ããã¸ã§ã¯ããä½æããç¶ã㦠"Manually select features" ãé¸æãã¦ä¸ãã
vue create my-project-name
åã¨ãã£ã¿ã«ãããµãã¼ã
TypeScript ã«ãã Vue ã¢ããªã±ã¼ã·ã§ã³ãéçºããããã«ãããã«å©ç¨ã§ãã TypeScript ã®ãµãã¼ããæä¾ãã Visual Studio Code ã使ç¨ãããã¨ãå¼·ãå§ãã¾ããåä¸ãã¡ã¤ã«ã³ã³ãã¼ãã³ã (SFC) ã使ç¨ãã¦ããå ´åãSFC å é¨ã§ TypeScript ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¨ä»ã®å¤ãã®åªããæ©è½ãæä¾ããç´ æ´ããã Vetur æ¡å¼µ ãå ¥æãã¦ãã ããã
WebStorm ã TypeScript 㨠Vue.js ã®ä¸¡æ¹ã«å¯¾ãã¦ããã«å©ç¨ã§ãããµãã¼ããæä¾ãã¦ãã¾ãã
åºæ¬çãªä½¿ãæ¹
Vue ã³ã³ãã¼ãã³ããªãã·ã§ã³å
é¨ã§ TypeScript ãåãé©åã«æ¨æ¸¬ã§ããããã«ããã«ã¯ãVue.component
ã¾ã㯠Vue.extend
ã§ã³ã³ãã¼ãã³ããå®ç¾©ããå¿
è¦ãããã¾ã:
import Vue from 'vue'
const Component = Vue.extend({
// åæ¨è«ãæå¹ã«ãã
})
const Component = {
// ããã¯åæ¨è«ãæã£ã¦ãã¾ããã
// ãªããªãããã㯠Vue ã³ã³ãã¼ãã³ãã®ãªãã·ã§ã³ã§ããã¨ãããã¨ãä¼ãããã¨ãã§ããªãããã§ãã
}
ã¯ã©ã¹ã¹ã¿ã¤ã« Vue ã³ã³ãã¼ãã³ã
ã³ã³ãã¼ãã³ãã宣è¨ããã¨ãã«ã¯ã©ã¹ãã¼ã¹ API ã使ç¨ããå ´åã¯ãå ¬å¼ã«ã¡ã³ããã³ã¹ããã¦ãã vue-class-component ã®ãã³ã¬ã¼ã¿ã使ç¨ã§ãã¾ã:
import Vue from 'vue'
import Component from 'vue-class-component'
// @Component ãã³ã¬ã¼ã¿ã¯ã¯ã©ã¹ã Vue ã³ã³ãã¼ãã³ãã§ãããã¨ã示ãã¾ã
@Component({
// ããã§ã¯ãã¹ã¦ã®ã³ã³ãã¼ãã³ããªãã·ã§ã³ã許å¯ããã¦ãã¾ã
template: '<button @click="onClick">Click!</button>'
})
export default class MyComponent extends Vue {
// åæãã¼ã¿ã¯ã¤ã³ã¹ã¿ã³ã¹ããããã£ã¨ãã¦å®£è¨ã§ãã¾ã
message: string = 'Hello!'
// ã³ã³ãã¼ãã³ãã¡ã½ããã¯ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¨ãã¦å®£è¨ã§ãã¾ã
onClick (): void {
window.alert(this.message)
}
}
ãã©ã°ã¤ã³ã§ä½¿ç¨ããããã®åæ¡å¼µ
ãã©ã°ã¤ã³ã¯ Vue ã®ã°ãã¼ãã«/ã¤ã³ã¹ã¿ã³ã¹ããããã£ãã³ã³ãã¼ãã³ããªãã·ã§ã³ã追å ãããã¨ãããã¾ãããã®ãããªå ´åãTypeScript ã§ãã®ãã©ã°ã¤ã³ã使ç¨ããã³ã¼ããã³ã³ãã¤ã«ããããã«ã¯åå®ç¾©ãå¿ è¦ã«ãªãã¾ãã幸ããTypeScript ã«ã¯ã¢ã¸ã¥ã¼ã«æ¡å¼µ (Module Augmentation) ã¨å¼ã°ããããã§ã«åå¨ããåãæ¡å¼µããæ©è½ãããã¾ãã
ä¾ãã°ãstring
åãã㤠$myProperty
ã¤ã³ã¹ã¿ã³ã¹ããããã£ãå®ç¾©ããã«ã¯:
// 1. æ¡å¼µããåãå®ç¾©ããåã«å¿
ã 'vue' ãã¤ã³ãã¼ãããããã«ãã¦ãã ãã
import Vue from 'vue'
// 2. æ¡å¼µãããåãå«ã¾ãããã¡ã¤ã«ãæå®ãã¦ãã ãã
// Vue ã®ã³ã³ã¹ãã©ã¯ã¿ã®å㯠types/vue.d.ts ã«å
¥ã£ã¦ãã¾ã
declare module 'vue/types/vue' {
// 3. æ¡å¼µãã Vue ãå®ç¾©ãã¾ã
interface Vue {
$myProperty: string
}
}
ä¸è¨ã®ã³ã¼ããï¼my-property.d.ts
ã®ãããªï¼åå®ç¾©ãã¡ã¤ã«ã¨ãã¦ããªãã®ããã¸ã§ã¯ãã«å«ããã¨ãVue ã¤ã³ã¹ã¿ã³ã¹ä¸ã§ $myProperty
ã使ç¨ã§ããããã«ãªãã¾ãã
var vm = new Vue()
console.log(vm.$myProperty) // ããã¯ãã¾ãã³ã³ãã¤ã«ããã
追å ã§ã°ãã¼ãã«ããããã£ãã³ã³ãã¼ãã³ããªãã·ã§ã³ãå®ç¾©ãããã¨ãã§ãã¾ã:
import Vue from 'vue'
declare module 'vue/types/vue' {
// `VueConstructor` ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ããã¦
// ã°ãã¼ãã«ããããã£ãå®ç¾©ã§ãã¾ã
interface VueConstructor {
$myGlobal: string
}
}
// ComponentOptions 㯠types/options.d.ts ã«å®ç¾©ããã¦ãã¾ã
declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
myOption?: string
}
}
ä¸è¨ã®åå®ç¾©ã¯æ¬¡ã®ã³ã¼ããã³ã³ãã¤ã«ã§ããããã«ãã¾ã:
// ã°ãã¼ãã«ããããã£
console.log(Vue.$myGlobal)
// 追å ã®ã³ã³ãã¼ãã³ããªãã·ã§ã³
var vm = new Vue({
myOption: 'Hello'
})
æ»ãå¤ã®åã«ã¢ããã¼ã·ã§ã³ãã¤ãã
Vue ã®å®£è¨ãã¡ã¤ã«ã¯å¾ªç°çãªæ§è³ªãæã¤ãããTypeScript ã¯ç¹å®ã®ã¡ã½ããã®åãæ¨è«ããã®ãå°é£ãªå ´åãããã¾ãããã®çç±ã®ãããrender
ã computed
ã®ã¡ã½ããã«æ»ãå¤ã®åã®ã¢ããã¼ã·ã§ã³ãä»ããå¿
è¦ãããããããã¾ããã
import Vue, { VNode } from 'vue'
const Component = Vue.extend({
data () {
return {
msg: 'Hello'
}
},
methods: {
// æ»ãå¤ã®åã® `this` ã®ããã«ãã¢ããã¼ã·ã§ã³ãå¿
è¦ã§ã
greet (): string {
return this.msg + ' world'
}
},
computed: {
// ã¢ããã¼ã·ã§ã³ãå¿
è¦ã§ã
greeting(): string {
return this.greet() + '!'
}
},
// `createElement` ã¯æ¨è«ããã¾ããã`render` ã¯æ»ãå¤ã®åãå¿
è¦ã§ã
render (createElement): VNode {
return createElement('div', this.greeting)
}
})
åæ¨è«ãã¡ã³ãã®è£å®ãæ©è½ãã¦ããªãå ´åãç¹å®ã®ã¡ã½ããã«ã¢ããã¼ã·ã§ã³ãä»ããã¨ãããã®åé¡ã«å¯¾å¦ã§ãã¾ãã--noImplicitAny
ãªãã·ã§ã³ã使ç¨ããã¨ããããã®ã¢ããã¼ã·ã§ã³ãä»ãããã¦ããªãã¡ã½ããã®å¤ããè¦ã¤ããã®ã«å½¹ç«ã¡ã¾ãã
ããããã£ã«ã¢ããã¼ã·ã§ã³ãã¤ãã
import Vue, { PropType } from 'vue'
interface ComplexMessage {
title: string,
okMessage: string,
cancelMessage: string
}
const Component = Vue.extend({
props: {
name: String,
success: { type: String },
callback: {
type: Function as PropType<() => void>
},
message: {
type: Object as PropType<ComplexMessage>,
required: true,
validator (message: ComplexMessage) {
return !!message.title;
}
}
}
})
ããªãã¼ã¿ãåæ¨è«ã§ããªããã¡ã³ãã®è£å®ãæ©è½ãã¦ããªãå ´åãæå¾ ãããåå¼æ°ä»ãã§ã¢ããã¼ã·ã§ã³ããã¨ãããã®åé¡ã«å¯¾å¦ã§ãã¾ãã