网站主页,如何用ps做网站界面,做三方网站多少钱,贵阳营销网站建设公司一、前言#xff1a;Vue 的两种 API 风格
Vue 提供了两种编写组件逻辑的方式#xff1a;组合式 API (Composition API) 和 选项式 API (Options API)。理解这两种方式的区别和适用场景#xff0c;对于 Vue 开发者至关重要。
为什么会有两种 API#xff1f;
选项式 APIVue 的两种 API 风格
Vue 提供了两种编写组件逻辑的方式组合式 API (Composition API) 和 选项式 API (Options API)。理解这两种方式的区别和适用场景对于 Vue 开发者至关重要。
为什么会有两种 API
选项式 APIVue 2 的传统方式按照选项data、methods 等组织代码组合式 APIVue 3 引入的新方式基于函数组合逻辑更适合复杂组件
二、选项式 API (Options API) 详解
1. 基本结构
script
export default {// 数据选项data() {return {count: 0,message: Hello Vue!}},// 计算属性computed: {reversedMessage() {return this.message.split().reverse().join()}},// 方法methods: {increment() {this.count}},// 生命周期钩子mounted() {console.log(组件已挂载)}
}
/scripttemplatedivp{{ message }}/pp反转: {{ reversedMessage }}/pbutton clickincrement计数: {{ count }}/button/div
/template2. 选项式 API 的特点
按选项组织代码将代码分为 data、methods、computed 等固定选项this 上下文通过 this 访问组件实例隐式响应式data 返回的对象自动成为响应式适合简单组件逻辑较少时结构清晰
3. 生命周期钩子
export default {beforeCreate() {},created() {},beforeMount() {},mounted() {},beforeUpdate() {},updated() {},beforeUnmount() {},unmounted() {}
}三、组合式 API (Composition API) 详解
1. 基本结构script setup 语法
script setup
import { ref, computed, onMounted } from vue// 响应式状态
const count ref(0)
const message ref(Hello Vue!)// 计算属性
const reversedMessage computed(() {return message.value.split().reverse().join()
})// 方法
function increment() {count.value
}// 生命周期钩子
onMounted(() {console.log(组件已挂载)
})
/scripttemplatedivp{{ message }}/pp反转: {{ reversedMessage }}/pbutton clickincrement计数: {{ count }}/button/div
/template2. 组合式 API 的特点
基于函数通过导入函数实现各种功能逻辑组合可以自由组织相关代码显式响应式需要明确使用 ref/reactive更好的 TypeScript 支持类型推断更自然逻辑复用可以提取和重用逻辑组合函数
3. 核心响应式 API
API用途选项式 API 对应物ref创建基本类型的响应式数据data 中的基本类型reactive创建对象的响应式代理data 中的对象computed创建计算属性computed 选项watch监听响应式数据变化watch 选项watchEffect自动追踪依赖的响应式效果-
4. 生命周期钩子对照
选项式 API组合式 APIbeforeCreate不需要直接写在 setup 中created不需要直接写在 setup 中beforeMountonBeforeMountmountedonMountedbeforeUpdateonBeforeUpdateupdatedonUpdatedbeforeUnmountonBeforeUnmountunmountedonUnmounted
四、两种 API 的深度对比
1. 代码组织方式
选项式 API
export default {data() { /*...*/ }, // 数据computed: { /*...*/ }, // 计算属性methods: { /*...*/ }, // 方法watch: { /*...*/ } // 监听器
}组合式 API
// 功能A相关代码
const { x, y } useFeatureA()// 功能B相关代码
const { a, b } useFeatureB()2. 逻辑复用对比
选项式 API 复用mixins
// mixin.js
export default {data() {return {mixinData: Mixin Data}},methods: {mixinMethod() { /*...*/ }}
}// 组件中使用
import myMixin from ./mixin.js
export default {mixins: [myMixin]
}组合式 API 复用组合函数
// useFeature.js
export function useFeature() {const state ref(null)function doSomething() { /*...*/ }return { state, doSomething }
}// 组件中使用
import { useFeature } from ./useFeature.js
const { state, doSomething } useFeature()3. 类型支持对比
选项式 API类型推断有限特别是在使用 mixins 时组合式 API天然支持 TypeScript类型推断更准确
五、何时使用哪种 API
选项式 API 适合
小型到中型项目简单组件开发熟悉 Vue 2 的开发者需要快速原型开发时
组合式 API 适合
大型复杂项目需要更好逻辑组织的组件需要逻辑复用的场景TypeScript 项目团队希望统一代码风格
六、组合式 API 进阶技巧
1. 自定义组合函数示例
// useCounter.js
import { ref } from vueexport function useCounter(initialValue 0) {const count ref(initialValue)function increment() {count.value}function decrement() {count.value--}function reset() {count.value initialValue}return {count,increment,decrement,reset}
}组件中使用
script setup
import { useCounter } from ./useCounterconst { count, increment } useCounter(10)
/scripttemplatebutton clickincrementCount: {{ count }}/button
/template2. 异步状态管理
script setup
import { ref } from vueconst data ref(null)
const loading ref(false)
const error ref(null)async function fetchData() {loading.value trueerror.value nulltry {const response await fetch(/api/data)data.value await response.json()} catch (err) {error.value err} finally {loading.value false}
}// 立即获取数据
fetchData()
/script3. 组件通信
!-- Parent.vue --
script setup
import { ref } from vue
import Child from ./Child.vueconst msg ref(Hello from parent)
/scripttemplateChild v-modelmsg /
/template!-- Child.vue --
script setup
defineProps([modelValue])
defineEmits([update:modelValue])
/scripttemplateinput:valuemodelValueinput$emit(update:modelValue, $event.target.value)/
/template七、迁移策略从选项式到组合式
1. 渐进式迁移
新组件使用组合式 API旧组件逐步重构混合使用在选项式组件中使用 setup 选项
2. 对应关系记忆卡
选项式 API组合式 APIthis不需要直接访问变量data()ref() 或 reactive()methods普通函数computedcomputed()watchwatch() 或 watchEffect()生命周期钩子onXxx() 系列函数propsdefineProps()emitsdefineEmits()mixins组合函数
八、常见问题解答
1. 两种 API 可以混用吗
可以但不推荐。在 Vue 3 中你可以在组件中使用 setup() 选项来使用组合式 API同时保留其他选项。
2. 组合式 API 更难学吗
对于 Vue 新手选项式 API 可能更容易上手。但有其他框架经验的开发者组合式 API 可能更直观。
3. 性能有差异吗
两种 API 在性能上没有显著差异组合式 API 在某些场景下可能有轻微优势。
4. 公司项目该用哪种
新项目推荐组合式 API旧项目可以逐步迁移。团队统一风格最重要。
九、总结
Vue 的两种 API 风格各有优势
选项式 API结构清晰学习曲线平缓适合简单场景组合式 API灵活强大便于逻辑复用和组织适合复杂场景
建议开发者
先掌握选项式 API 理解 Vue 核心概念逐渐过渡到组合式 API根据项目需求和个人偏好选择合适的方式大型项目推荐统一使用组合式 API
组合式 API 代表了 Vue 的未来发展方向特别是对于复杂应用和需要良好 TypeScript 支持的项目。选项式 API 仍会长期支持适合维护旧项目和简单场景。
创作不易如果您都看到这里了可以给我一个点赞、收藏并关注一下么您的支持与喜爱是激励我创作的最大动力
如果内容有误请及时联系我进行修改 文章转载自: http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.jrhcp.cn.gov.cn.jrhcp.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.rfbt.cn.gov.cn.rfbt.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.hgtr.cn.gov.cn.hgtr.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.zbnts.cn.gov.cn.zbnts.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.hrzymy.com.gov.cn.hrzymy.com http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.cjrmf.cn.gov.cn.cjrmf.cn http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.ppzgr.cn.gov.cn.ppzgr.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.clpkp.cn.gov.cn.clpkp.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.qwmsq.cn.gov.cn.qwmsq.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn