实用!最新的几个 Vue 3 重要特性提案

在几天前开启的 SFC Improvements #182 中,yyx990803 提交了 3 个改进开发者体验的征求意见稿。javascript

虽然看上去都不是体量很大的改动,但都至关实用,开发者谁用谁知道!一块儿来先睹为快:css

1. <component> 组件导入语法糖

在目前的写法中,要引入其它组件,就得先在 <script> 中 import,再将其模块名放入组件的 components: {} 属性对象中。好比:html

<template>
  <Foo/>
</template>

<script> import Foo from './Foo.vue' export default { components: { Foo } } </script>
复制代码

又或者异步组件:前端

<template>
  <Foo/>
</template>

<script> import { defineAsyncComponent } from 'vue' export default { components: { Foo: defineAsyncComponent(() => import('./Foo.vue')) } } </script>
复制代码

这样写起来既费力,又在使用新的 Composition API 显得不那么“新”,仍是要 props、components 写一大堆。vue

有鉴于此,新的提案设计成这样:java

<component src="./Foo.vue"/>
<component async src="./Bar.vue"/>
<component src="./Baz.vue" as="Qux" />

<template>
  <Foo/>
  <Bar/>
  <Qux/>
</template>
复制代码

2. 简化 Composition API 的 <script setup>

正如上面提到过的,在使用新的 Composition API 时,对于不用引入其它组件的、相对简单的那些组件来讲,只包含一个 setup() 的组件声明对象也很常见。好比:git

import { ref } from 'vue'

export default {
  setup() {
    const count = ref(0)
    const inc = () => count.value++

    return {
      count,
      inc
    }
  }
}
复制代码

新提案将其简化为:github

<template>
  <button @click="inc">{{ count }}</button>
</template>

<script setup> import { ref } from 'vue' export const count = ref(0) export const inc = () => count.value++ </script>
复制代码

另外,<script setup> 中将隐式注入几个对象:异步

  • $props
  • $attrs
  • $slots
  • $emit

好比以前的写法为:async

import { watchEffect } from 'vue'

export default {
  setup($props, { emit: $emit }) {
    watchEffect(() => console.log($props.msg))
    $emit('foo')
    return {}
  }
}
复制代码

简化后 <script setup> 中写为:

import { watchEffect } from 'vue'

watchEffect(() => console.log($props.msg))
$emit('foo')
复制代码

声明其它组件选项

使用 <script setup> 后,若是还想手动指定 props 等组件选项,能够用一个 export default 解决,即:

<script setup> import { computed } from 'vue' export default { props: { msg: String }, inheritAttrs: false } export const computedMsg = computed(() => $props.msg + '!!!') </script>
复制代码

结合 TypeScript

要声明 $props$emit 等隐式对象的 TS 类型,采用下面的语法:

<script setup lang="ts"> import { computed } from 'vue' // 使用 TypeScript 语法声明 props declare const $props: { msg: string } export const computedMsg = computed(() => $props.msg + '!!!') </script>
复制代码

重要的是,这些 TS 声明会被自动编译成等价的运行时声明。如以上代码将转化为:

<script lang="ts"> import { computed, defineComponent } from 'vue' type __$props__ = { msg: string } export default defineComponent({ props: (['msg'] as unknown) as undefined, setup($props: __$props__) { const computedMsg = computed(() => $props.msg + '!!!') return { computedMsg } } }) </script>
复制代码

这样也避免了为静态检查和运行时写两遍重复的声明。

3. 状态驱动的 <style> 中 CSS 变量注入

以往要根据状态或属性来影响样式的话,仍是比较麻烦的。首先要提早在 <style> 中写好几种 className 对应的样式,再利用脚本动态在模版中赋相应的 className 才行。

新提案结合原生的 CSS variables 特性,提供了更方便的联动方案:

<template>
  <div class="text">hello</div>
</template>

<script> export default { data() { return { color: 'red' } } } </script>

<style :vars="{ color }"> .text { color: var(--color); } </style>
复制代码

总结

再次重复,这几个特性仅仅是最新的提案,并不必定是最终的样子;但鉴于其实用性,开发者仍是有必要早作了解,多提建议。

提案地址以下:github.com/vuejs/rfcs/…



--End--

查看更多前端好文
请搜索 fewelife 关注公众号

转载请注明出处

相关文章
相关标签/搜索