1. <component>
组件导入语法糖
在目前的写法中,要引入其它组件,就得先在 <script>
中 import,再将其模块名放入组件的 components: {}
属性对象中。比如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< template > < Foo /> </ template > < script > import Foo from './Foo.vue' export default { components: { Foo } } </ script > 复制代码 |
又或者异步组件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< template > < Foo /> </ template > < script > import { defineAsyncComponent } from 'vue' export default { components: { Foo: defineAsyncComponent(() => import('./Foo.vue')) } } </ script > 复制代码 |
这样写起来既费力,又在使用新的 Composition API 显得不那么“新”,还是要 props、components 写一大堆。
有鉴于此,新的提案设计成这样:
1
2
3
4
5
6
7
8
9
10
|
< 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()
的组件声明对象也很常见。比如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import { ref } from 'vue' export default { setup() { const count = ref(0) const inc = () => count.value++ return { count, inc } } } 复制代码 |
新提案将其简化为:
1
2
3
4
5
6
7
8
9
10
11
|
< 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
比如之前的写法为:
1
2
3
4
5
6
7
8
9
10
|
import { watchEffect } from 'vue' export default { setup($props, { emit: $emit }) { watchEffect(() => console.log($props.msg)) $emit('foo') return {} } } 复制代码 |
简化后 <script setup>
中写为:
1
2
3
4
5
|
import { watchEffect } from 'vue' watchEffect(() => console.log($props.msg)) $emit('foo') 复制代码 |
声明其它组件选项
使用 <script setup>
后,如果还想手动指定 props
等组件选项,可以用一个 export default
解决,即:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< script setup> import { computed } from 'vue' export default { props: { msg: String }, inheritAttrs: false } export const computedMsg = computed(() => $props.msg + '!!!') </ script > 复制代码 |
结合 TypeScript
要声明 $props
或 $emit
等隐式对象的 TS 类型,采用下面的语法:
1
2
3
4
5
6
7
8
9
10
11
|
< script setup lang = "ts" > import { computed } from 'vue' // 使用 TypeScript 语法声明 props declare const $props: { msg: string } export const computedMsg = computed(() => $props.msg + '!!!') </ script > 复制代码 |
重要的是,这些 TS 声明会被自动编译成等价的运行时声明。如以上代码将转化为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< 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 特性,提供了更方便的联动方案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< template > < div class = "text" >hello</ div > </ template > < script > export default { data() { return { color: 'red' } } } </ script > < style :vars = "{ color }" > .text { color: var(--color); } </ style > 复制代码 |
分类:
vue 3.x相关
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗