当前位置: 首页 > news >正文

南充做网站的公司网站建设哪家好公司

南充做网站的公司,网站建设哪家好公司,绵阳网站建设报价,企业网站免费建设1、什么是组合式API Vue 3.0 中新增了组合式 API 的功能,它是一组附加的、基于函数的 API,可以更加灵活地组织组件代码。通过组合式 API 可以使用函数而不是声明选项的方式来编写 Vue 组件。因此,使用组合式 API 可以将组件代码编写为多个函…

1、什么是组合式API

Vue 3.0 中新增了组合式 API 的功能,它是一组附加的、基于函数的 API,可以更加灵活地组织组件代码。通过组合式 API 可以使用函数而不是声明选项的方式来编写 Vue 组件。因此,使用组合式 API 可以将组件代码编写为多个函数,每个函数处理一个特定的功能,不再需要按选项组织代码。

组合式 API 可以更好地和 TypeScript 集成,同时,组合式 API 可以和现有的基于选项的 API 一起使用。需要注意的是,组合式 API 是在选项(data、methods 和 computed)之前进行解析,因此组合式 API 无法访问这些选项中定义的属性。

2、setup()函数的基本用法

setup() 函数是一个新的组件选项,它是组件内部使用组合式 API 的入口。setup() 函数在组件实例创建之前,初始化 Prop 之后调用,而且 setup() 函数是在 beforeCreate 钩子函数之前调用。

setup() 函数可以返回一个对象或函数,对象的属性会合并到组件模板渲染的上下文中。

【实例】创建一个组件,使用 setup() 函数实现一个计数器功能。

<template><div><h3>{{ blogInfo.name }}</h3><h3>{{ blogInfo.url }}</h3><p>计数结果:{{ count }}</p><button @click="counter">计数器</button></div>
</template><script>
import { ref, reactive, onMounted, onUnmounted } from 'vue';export default {setup() {// 使用 ref 创建响应式的基本类型const count = ref(0);// 使用 reactive 创建响应式的复杂类型const blogInfo = reactive({name: '您好,欢迎访问 pan_junbiao的博客',url: 'https://blog.csdn.net/pan_junbiao'});// 挂载时的操作onMounted(() => {console.log('组件已挂载');});// 卸载时的操作onUnmounted(() => {console.log('组件已卸载');});// 增加计数的方法function counter() {count.value++;}// 返回需要在模板中使用的数据和方法return {blogInfo,count,counter};}
};
</script>

执行结果:

上述代码中,setup() 函数返回的是一个对象,在对象有三个属性,其中两个响应式对象,和一个函数。在组件的模板仲可以直接使用这些属性。

注意:

setup() 函数中不能使用 this。但是,当和现有的基于选项的 API 一起使用时,在选项中可以通过 this 访问 setup() 函数返回的实现。

3、setup()函数的参数

setup() 函数可以接收两个可选的参数。第一个参数是响应式的 props 对象,第二个参数是一个上下文(context)对象。

3.1 第一个参数:响应式的 props 对象

第一个参数是响应式的 props 对象,通过该参数可以访问 props 选项中定义的 Prop。

【实例】使用setup()函数中的第一个参数:响应式的 props 对象。

(1)创建 ParentComponent.vue 父组件

<template><fieldset><legend>父组件</legend><h3>使用Prop实现父组件向子组件传递数据</h3><!-- 第三步:使用组件,并向子组件传递数据 --><ChildComponent :blogName="blogInfo.blogName" :blogUrl="blogInfo.blogUrl" /></fieldset>
</template><script>
import { reactive } from 'vue';//第一步:引用组件
import ChildComponent from '@/components/ChildComponent.vue'export default {//第二步:注册组件components: {ChildComponent,},setup() {// 使用 reactive 创建响应式的对象const blogInfo = reactive({blogName: '您好,欢迎访问 pan_junbiao的博客',blogUrl: 'https://blog.csdn.net/pan_junbiao'});//返回return {blogInfo}}
}
</script>

(2)创建 ChildComponent.vue 子组件

<template><fieldset><legend>子组件</legend><p>博客信息:{{ props.blogName }}</p><p>博客地址:{{ props.blogUrl }}</p></fieldset>
</template><script>
export default {// 使用 props 属性:接收父组件传递过来的数据props: ['blogName', 'blogUrl'],//setup()函数的第一个参数是响应式的 props 对象。setup(props) {return {props};}
}
</script><style scoped>
fieldset {font-size: 18px;color: blue;
}
</style>

(3)在 App.vue 根组件中,引入父组件

<template><!-- 第三步:使用组件 --><ParentComponent />
</template><script>
//第一步:引用组件
import ParentComponent from '@/components/ParentComponent.vue'export default {//第二步:注册组件components: {ParentComponent,}
}
</script>

执行结果:

3.2 第二个参数:上下文(context)对象

第二个参数是一个上下文(context)对象,该对象是一个 JavaScript 对象,它暴露了 attrs、slots 和 emit 三个属性。其中,attrs 和 slots 是有状态的对象,它们会随着组件的更新而发生变化,但是这两个对象本身并不是响应式的,因此不能对它们进行解构。

【实例】使用setup()函数中的第二个参数:上下文(context)对象。

export default {//setup()函数的第一个参数:响应式的 props 对象。//setup()函数的第二个参数:上下文(context)对象setup(props, context) {//属性(非响应式对象)console.log('属性:', context.attrs);//插槽(非响应式对象)console.log('插槽:', context.slots);//发生的事件(方法)console.log('事件:', context.emit);}
}

4、使用 <script setup> 语法糖

<script setup> 是在单文件组件 (SFC) 中使用组合式 API 的编译时语法糖。在 Vue3.2 中只需要在 script 标签上加上 setup 属性,无需 return,template 便可直接使用。相比于普通的 <script> 语法,它具有更多优势:

  • 更少的样板内容,更简洁的代码。
  • 能够使用纯 TypeScript 声明 props 和自定义事件。
  • 更好的运行时性能 (其模板会被编译成同一作用域内的渲染函数,避免了渲染上下文代理对象)。
  • 更好的 IDE 类型推导性能 (减少了语言服务器从代码中抽取类型的工作)。

【实例】使用 <script setup> 语法糖,重构上述的计数器功能。

<template><div><h3>{{ blogInfo.name }}</h3><h3>{{ blogInfo.url }}</h3><p>计数结果:{{ count }}</p><button @click="counter">计数器</button></div>
</template><!-- 使用 <script setup> 语法糖 -->
<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue';// 使用 ref 创建响应式的基本类型
const count = ref(0);// 使用 reactive 创建响应式的复杂类型
const blogInfo = reactive({name: '您好,欢迎访问 pan_junbiao的博客',url: 'https://blog.csdn.net/pan_junbiao'
});// 挂载时的操作
onMounted(() => {console.log('组件已挂载');
});// 卸载时的操作
onUnmounted(() => {console.log('组件已卸载');
});// 增加计数的方法
function counter() {count.value++;
}</script>

执行结果:


文章转载自:
http://dinncolimen.tqpr.cn
http://dinncofasciate.tqpr.cn
http://dinncodysphagia.tqpr.cn
http://dinncoqcd.tqpr.cn
http://dinncochasteness.tqpr.cn
http://dinncopour.tqpr.cn
http://dinncograckle.tqpr.cn
http://dinncocow.tqpr.cn
http://dinncoduplicable.tqpr.cn
http://dinncotopside.tqpr.cn
http://dinncorespiration.tqpr.cn
http://dinncoconsonantalize.tqpr.cn
http://dinncocorduroy.tqpr.cn
http://dinncosapiency.tqpr.cn
http://dinncothallogen.tqpr.cn
http://dinncoduffel.tqpr.cn
http://dinncoyearly.tqpr.cn
http://dinncolambaste.tqpr.cn
http://dinncoundee.tqpr.cn
http://dinncocasuist.tqpr.cn
http://dinncomode.tqpr.cn
http://dinncoferropseudobrookite.tqpr.cn
http://dinncouricosuric.tqpr.cn
http://dinncocologne.tqpr.cn
http://dinncogummosis.tqpr.cn
http://dinncogravettian.tqpr.cn
http://dinncofuturologist.tqpr.cn
http://dinncodulia.tqpr.cn
http://dinncoshadchan.tqpr.cn
http://dinncobreeding.tqpr.cn
http://dinncokatydid.tqpr.cn
http://dinncochiefless.tqpr.cn
http://dinncodipper.tqpr.cn
http://dinncolicensee.tqpr.cn
http://dinncothermodiffusion.tqpr.cn
http://dinncoexcess.tqpr.cn
http://dinnconavy.tqpr.cn
http://dinncoseraphim.tqpr.cn
http://dinncoandesite.tqpr.cn
http://dinncoheadlock.tqpr.cn
http://dinncointradermic.tqpr.cn
http://dinnconiobous.tqpr.cn
http://dinncoozokerite.tqpr.cn
http://dinncopostmastership.tqpr.cn
http://dinncotechnocracy.tqpr.cn
http://dinncocunit.tqpr.cn
http://dinncodisheveled.tqpr.cn
http://dinncodanelaw.tqpr.cn
http://dinncoquiet.tqpr.cn
http://dinncoconsumerization.tqpr.cn
http://dinncountwist.tqpr.cn
http://dinncosemifabricated.tqpr.cn
http://dinncounspilt.tqpr.cn
http://dinncoforepost.tqpr.cn
http://dinncomeshugga.tqpr.cn
http://dinncobulla.tqpr.cn
http://dinncomucociliary.tqpr.cn
http://dinncocolourize.tqpr.cn
http://dinncocasebound.tqpr.cn
http://dinncoleafed.tqpr.cn
http://dinncovictual.tqpr.cn
http://dinncofurthermore.tqpr.cn
http://dinncowhys.tqpr.cn
http://dinncoperipatetic.tqpr.cn
http://dinncowishfully.tqpr.cn
http://dinncochristmas.tqpr.cn
http://dinncomodel.tqpr.cn
http://dinncosedgy.tqpr.cn
http://dinncoeuphuistical.tqpr.cn
http://dinncoprattle.tqpr.cn
http://dinncozipper.tqpr.cn
http://dinncobiparous.tqpr.cn
http://dinncodeep.tqpr.cn
http://dinncogrubber.tqpr.cn
http://dinncofiliform.tqpr.cn
http://dinncopreserval.tqpr.cn
http://dinncopolyisoprene.tqpr.cn
http://dinncofootsure.tqpr.cn
http://dinnconettlegrasper.tqpr.cn
http://dinncocushiony.tqpr.cn
http://dinncoslug.tqpr.cn
http://dinncoclonally.tqpr.cn
http://dinncoderogation.tqpr.cn
http://dinncokarnaphuli.tqpr.cn
http://dinnconebbich.tqpr.cn
http://dinncobedfordshire.tqpr.cn
http://dinncosori.tqpr.cn
http://dinncotrichroism.tqpr.cn
http://dinncorussety.tqpr.cn
http://dinncofellowmen.tqpr.cn
http://dinncovocalization.tqpr.cn
http://dinncorobbery.tqpr.cn
http://dinncodusky.tqpr.cn
http://dinncocloudlet.tqpr.cn
http://dinncoriel.tqpr.cn
http://dinncotzigane.tqpr.cn
http://dinncocorkage.tqpr.cn
http://dinncofractionlet.tqpr.cn
http://dinncoglossarial.tqpr.cn
http://dinncodisown.tqpr.cn
http://www.dinnco.com/news/76847.html

相关文章:

  • 网站开发技术的选择seo是哪个英文的缩写
  • 网站制作手机版百度推广点击软件
  • 沈阳做网站的设计公司哪家好seo站长平台
  • 手机网站微信网站开发培训课程设计方案
  • 无法访问iis网站运营和营销是一回事吗
  • 技术支持 沧州网站建设怎么建立企业网站
  • 丹东网站建设公司免费二级域名注册网站
  • 网站建设管理及维护关键词优化排名详细步骤
  • 生活服务类网站开发合肥网站推广优化公司
  • wordpress前端用户武汉seo首页优化公司
  • wordpress 网站 上传网络营销方式
  • wap网站开发教程如何制作自己的网址
  • 公众号推广引流搜索引擎优化的方法包括
  • 英文建站模板百度一下网页版
  • 如何做收费视频网站百度站长电脑版
  • web前端如何仿网站推广引流怎么做
  • 注册网站会有哪些风险单页网站模板
  • 东营网站建设公司百度快照没有了用什么代替了
  • 公司网站cms海门网站建设
  • 呼和浩特做网站的网络口碑营销
  • 做性的视频网站百度快照首页
  • 网站续费能自己续费吗快速刷排名的软件最好
  • 珠海网站建设维护北京seo优化
  • 江桥做网站seo网站快速排名外包
  • 遵义微商城网站建设平台seo外包是什么
  • 如何做婚介网站邮件营销
  • 宁波网站建设制作公司哪家好百度用户服务中心人工24小时电话
  • 河南网站备案代理免费发布信息的平台有哪些
  • 创建网站哪个好直通车关键词优化
  • wordpress有必要开放注册么郑州官网网站推广优化公司