hooks翻译过来是钩子的意思,这个可能有一些模糊,简单点说hooks就是一个函数(能够复用的函数)
javascript
其实TypeScript并非一门新的语言,它是 JavaScript 类型的超集,typeScript那并非一个新语言,能够理解为增强JavaScript的buff,TypeScript最大的优点源于强大的类型系统,还有就是在编写代码的时候就能够检测出咱们可能由于粗心形成的没必要要的错误css
npm install -g @vue/cli
# OR
yarn global add @vue/cli复制代码
.
├── README.md
├── babel.config.js
├── package.json
├── postcss.config.js
├── public│
├── favicon.ico│
└── index.html├── src│
├── App.vue│
├── assets│
│ └── logo.png│
├── components│
│ └── HelloWorld.vue│
├── main.ts│
├── router│
│ └── index.ts│
├── shims-tsx.d.ts 容许你以 .tsx结尾的文件,在 Vue项目中编写 jsx代码│
├── shims-vue.d.ts 主要用于 TypeScript 识别 .vue 文件│
├── store│
│ └── index.ts│
└── views│
├── About.vue│
└── Home.vue
├── tsconfig.json
└── yarn.lock复制代码
<template>
<div class="hello">
<h1>{{ msg }}</h1>
...
</div>
</template>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
@Componentexport default class HelloWorld extends Vue {
@Prop() private msg!: string;
}
</script>复制代码
Vue3.0尚未正式发布,可是官方提供了Vue 2.x 可以提早体验此API的库@vue/composition-api
html
npm i @vue/composition-api -S复制代码
import Vue from 'vue'
import VueCompositionApi from '@vue/composition-api'
Vue.use(VueCompositionApi)复制代码
<template>
<div id="app">
</div>
</template>
<script lang="ts">
import { createComponent } from '@vue/composition-api'
export default createComponent({ ...})
</script>
复制代码
实例:这是VueConf 2019尤大大在介绍的时候给出的一个例子,改为ts版本前端
<template>
<div id="app">
<div class="hooks-one">
<h2>{{ msg }}</h2>
<p>count is {{ count }}</p>
<p>plusOne is {{ plusOne }}</p>
<button @click="increment">count++</button>
</div>
<!-- <router-view/> -->
</div>
</template>
<script lang="ts">
import { ref, computed, watch, onMounted, Ref, createComponent } from '@vue/composition-api'
interface Props {
name: string
}
export default createComponent({
props: {
name: {
type: String,
default: 'ssss'
}
},
components: {},
setup (props: Props, context) {
const count: Ref<number> = ref(0)
// computed
const plusOne = computed(() => count.value + 1)
// method
const increment = () => {
count.value++
}
// watch
watch(() => count.value * 2, val => {
console.log(`count * 2 is ${val}`)
})
// 生命周期
onMounted(() => {
console.log('onMounted')
})
// expose bindings on render context
return {
count,
plusOne,
increment,
msg: `hello ${props.name}`
}
}
})
</script>复制代码
详解:vue
<template>
<div id="app">
{{ msg }}
{{ count }}
</div>
</template>
<script lang="ts">
import { createComponent } from '@vue/composition-api'
export default createComponent({
props: {
name: String
},
setup (props) {
const count: Ref<number> = ref(0)
return {
msg: `hello ${props.name}`,
count
}
}
})
</script>复制代码
当前,咱们使用所谓的 Options API 构建组件。为了向 Vue 组件添加逻辑,咱们填充(可选)属性,例如 data、methods、computed等。这种方法的最大缺点是其自己并非有效的 JavaScript 代码。你须要确切地知道模板中能够访问哪些属性以及 this 关键字的行为。在后台,Vue 编译器须要将此属性转换为工做代码。所以咱们没法从自动建议或类型检查中受益。
java
<template>
<div id="app">
<div class="hooks-one">
{{state.double}}
{{state.count}}
<p>count is {{ count }}</p>
<button @click="increment">count++</button>
</div>
<!-- <router-view/> -->
</div>
</template>
<script lang="ts">
import { ref, computed, reactive, Ref, createComponent } from '@vue/composition-api'
interface State { count: number, double: number}
export default createComponent({ setup (props, context) {
const count: Ref<number> = ref(0)
const state: State = reactive({
count: 0,
double: computed(() => state.count * 2)
})
// method
const increment = () => {
count.value++ // 须要.value获取,改变
state.count++
}
// expose bindings on render context
return {
increment,
state
}
}
})
</script>复制代码
import { onMounted, onUpdated, onUnmounted, createComponent } from '@vue/composition-api';
export default createComponent({
setup() {
onMounted(() => {
console.log('mounted!');
});
onUpdated(() => {
console.log('updated!');
});
// destroyed 调整为 unmounted
onUnmounted(() => {
console.log('unmounted!');
});
},
});复制代码