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

网站建设建设百度网盘优化

网站建设建设,百度网盘优化,android应用开发教程,济南商城网站制作目录开篇:1.什么是setup2.setup怎么使用3.setup中包含的生命周期函数3.setup相关参数4.setup特性总结总结开篇: 从vue2升级 vue3,vue3是可以兼容vue2。所以v3可以采用v2的选项式api,但是v2不能使用v3的组合式api,由于…

目录

  • 开篇:
  • 1.什么是setup
  • 2.setup怎么使用
  • 3.setup中包含的生命周期函数
  • 3.setup相关参数
  • 4.setup特性总结
  • 总结

开篇:

从vue2升级 vue3,vue3是可以兼容vue2。所以v3可以采用v2的选项式api,但是v2不能使用v3的组合式api,由于选项式api一个变量存在于多处,如果出现问题,就需要去涵盖多个函数。项目越大,排查的难度也就越大。


1.什么是setup

  • setup用来写组合式api,从生命周期的角度,相当于取代了beforeCreate()

2.setup怎么使用

1.setup() / {}  内部的属性和方法,必须使用return暴露出来。将属性挂载到实例上,否则没有办法使用。
2.语法糖:写在script开始标签中,内部的属性和方法,无需return暴露;无法和选项式api混用	
3. 钩子函数可以和setup并列存在,	setup不能调用生命周期相关的函数。
生命周期相关函数可以调用setup相关的属性和方法,可以使用this或者嵌套存在。

3.setup中包含的生命周期函数

onBeforeMount——挂载开始前调用
onMounted——挂载后调用
onBeforeUpdate——当响应数据改变,且重新渲染前调用
onUpdated——重新渲染后调用
onBeforeUnmount——Vue实例销毁前调用
onUnmounted——实例销毁后调用
onActivated——当keep-alive组件被激活时调用
onDeactivated——当keep-alive组件取消激活时调用
onErrorCaptured——从子组件中捕获错误时调用

相关代码:

//选项式写法
<script>
export default{props: ['subtitle'],data: () => ({age: 30}),methods: {showMessage() {console.log('函数 HELLO');  }},// 组件实例话之前// 可以访问 props 的数据的// 不能访问组件的实例 this 中的数据源和函数等// 不能访问组件中的视图DOM元素beforeCreate() {console.log('----------------------------')console.log('beforeCreate 组件实例话之前')console.log(this.$props.subtitle)console.log('不能访问组件的实例 this 中的数据源和函数等');console.log('不能访问组件中的视图DOM元素');// console.log(this.age)// this.showMessage()// console.log(document.getElementById('title').innerHTML)},// 组件实例话之后// 可以访问组件中的数据,函数,自定义的属性等// 不能访问组件中的视图DOM元素created() {console.log('----------------------------')console.log('created 组件实例话之后')console.log(this.$props.subtitle)console.log(this.age)this.showMessage()console.log('不能访问组件中的视图DOM元素');// console.log(document.getElementById('title').innerHTML)},// 组件视图渲染之前// 可以访问组件中的数据,函数,自定义的属性等// 不能访问组件中的视图DOM元素beforeMount() {console.log('----------------------------')console.log('beforeMount 组件视图渲染之前')console.log(this.$props.subtitle)console.log(this.age)this.showMessage()console.log('不能访问组件中的视图DOM元素');// console.log(document.getElementById('title').innerHTML)},// 组件视图渲染之后// 可以访问组件中的数据,函数,自定义的属性等// 不能访问组件中的视图DOM元素mounted() {console.log('----------------------------')console.log('mounted 组件视图渲染之后')console.log(this.$props.subtitle)console.log(this.age)this.showMessage()console.log(document.getElementById('title').innerHTML)},// 数据源发生改变,视图重新渲染前// 可以访问组件中的数据,函数,自定义的属性等// 可访问重新渲染的 DOM 元素之前的状态beforeUpdate() {console.log('----------------------------')console.log('beforeUpdate 数据源发生改变,视图重新渲染前')console.log(this.$props.subtitle)console.log(this.age)this.showMessage()console.log(document.getElementById('title').innerHTML)},// 数据源发生改变,视图重新渲染后// 可以访问组件中的数据,函数,自定义的属性等// 可访问重新渲染的 DOM 元素之后的状态updated() {console.log('----------------------------')console.log('updated 数据源发生改变,视图重新渲染后')console.log(this.$props.subtitle)console.log(this.age)this.showMessage()console.log(document.getElementById('title').innerHTML)},// 组件在卸载之前// 可以访问组件中的数据,函数,自定义的属性等// 可访组件视图的 DOM 元素beforeUnmount() {console.log('----------------------------')console.log('beforeUnmount 组件在卸载之前')console.log(this.$props.subtitle)console.log(this.age)this.showMessage()console.log(document.getElementById('title').innerHTML)},// 组件已卸载// 可以访问组件中的数据,函数,自定义的属性等// 不可访组件视图的 DOM 元素unmounted(){console.log('----------------------------')console.log('unmounted 组件已卸载')console.log(this.$props.subtitle)console.log(this.age)this.showMessage()console.log('不能访问组件中的视图DOM元素');// console.log(document.getElementById('title').innerHTML)}
}
</script><template><h3 id="title"><i>年龄:{{ age }}</i></h3><button @click="(age = 70)">年龄改成 70</button><button @click="(age = 30)">年龄改成 30</button>
</template>
// 组合式写法
<script setup>
import { onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref } from 'vue';let age = ref(30)function showMessage() {console.log('HELLO')
}// 组件视图渲染之前
// 能访问组件实例的东西(数据源、函数等)
// 但是不能访问组件视图中的 DOM 元素
onBeforeMount(() => {console.log('------------------------')console.log('onBeforeMount 组件视图渲染之前(生命周期钩子)')console.log(age.value)showMessage()console.log('不能访问组件视图中的 DOM 元素');// console.log(document.getElementById('title').innerHTML)
})// 组件视图渲染之后
// 能访问组件实例的东西(数据源、函数等)
// 可以访问组件视图中的 DOM 元素
onMounted(() => {console.log('------------------------')console.log('onMounted 组件视图渲染之后(生命周期钩子)')console.log(age.value)showMessage()console.log(document.getElementById('title').innerHTML)
})// 数据源发生变化,组件视图重新渲染之前
// 能访问组件实例的东西(数据源、函数等)
// 能访问组件视图渲染之前的 DOM 元素
onBeforeUpdate(() => {console.log('------------------------')console.log('onBeforeUpdate 数据源发生变化,组件视图重新渲染之前(生命周期钩子)')console.log(age.value)showMessage()console.log(document.getElementById('title').innerHTML)
})// 数据源发生变化,组件视图重新渲染之后
// 能访问组件实例的东西(数据源、函数等)
// 能访问组件视图渲染之后的 DOM 元素
onUpdated(() => {console.log('------------------------')console.log('onUpdated 数据源发生变化,组件视图重新渲染之后(生命周期钩子)')console.log(age.value)showMessage()console.log(document.getElementById('title').innerHTML)
})// 组件卸载之前
// 能访问组件实例的东西(数据源、函数等)
// 能访问组件视图 DOM 元素
onBeforeUnmount(() => {console.log('------------------------')console.log('onBeforeUnmount 组件卸载之前(生命周期钩子)')console.log(age.value)showMessage()console.log(document.getElementById('title').innerHTML)
}) // 组件卸载之后
// 能访问组件实例的东西(数据源、函数等)
// 不能访问组件视图 DOM 元素
onUnmounted(() => {console.log('------------------------')console.log('onUnmounted 组件卸载之后(生命周期钩子)')console.log(age.value)showMessage()console.log('不能访问组件视图中的 DOM 元素');// console.log(document.getElementById('title').innerHTML)
}) </script><template><h3 id="title"><i>年龄:{{ age }}</i></h3><button @click="(age = 70)">年龄改成 70</button><button @click="(age = 30)">年龄改成 30</button>
</template>

3.setup相关参数

  • 使用setup时,她讲接受两个参数:props和context
    • props:
      1. 表示父组件给子组件传的数据;
      2. props是响应式的,当数据发送改变时,自动更新
      3. 因为props是响应式的,不能使用es6的解构,会消除响应式特性(使用toRefs)
    • context:
      1. context 上下文环境。其中包括了属性,插槽,自定义事件三部分
      2. attrs:是一个非响应式对象,只要接受no-props属性。经常用来传递一些样式/标签特有属性
      3. solts:是一个Proxy对象,其中slots.default()获取到一个数组。数组长度表示插槽的数量,
        数组中的元素时插槽的内容。
      4. emit:因为setup没有this,所以使用emit开替换之前的this.$emit。用于子传父时,自定义事件的触发 示例:emit(“自定义事件名”,传递的值)

4.setup特性总结

​1. 这个函数会在created之前执行
2. setup内部没有this,不能挂载相关的东西
3. setup内部的属性和方法,必须return暴露出来。(语法糖不需要)
4. setup内部的属性都不是响应式的
5. setup不能调用生命周期相关函数,但生命周期相关函数可以调用setup

总结

以上就是Vue3.0 setup的使用及作用。希望本篇文章能够帮助到你,不懂得可以评论区或者私信问我,我也会一 一解答。谢谢观看!
我的其他文章:https://blog.csdn.net/m0_60970928?type=blog


文章转载自:
http://dinncophytol.tqpr.cn
http://dinncofiot.tqpr.cn
http://dinncoclindamycin.tqpr.cn
http://dinncodisbranch.tqpr.cn
http://dinncodetainment.tqpr.cn
http://dinncoworkstation.tqpr.cn
http://dinncofrigidaria.tqpr.cn
http://dinncoimposturing.tqpr.cn
http://dinncoswan.tqpr.cn
http://dinncodumbwaiter.tqpr.cn
http://dinncohammering.tqpr.cn
http://dinncohyperirritability.tqpr.cn
http://dinncoanuresis.tqpr.cn
http://dinncosee.tqpr.cn
http://dinncoacanthoid.tqpr.cn
http://dinncoplagiocephalism.tqpr.cn
http://dinncogeographic.tqpr.cn
http://dinncothinnet.tqpr.cn
http://dinncothere.tqpr.cn
http://dinncoexterminative.tqpr.cn
http://dinncoedificatory.tqpr.cn
http://dinncotitlark.tqpr.cn
http://dinncotreadless.tqpr.cn
http://dinncohypopiesis.tqpr.cn
http://dinncoburgee.tqpr.cn
http://dinncoauricula.tqpr.cn
http://dinncoshoshoni.tqpr.cn
http://dinncosloak.tqpr.cn
http://dinncointerjacent.tqpr.cn
http://dinncomassive.tqpr.cn
http://dinncodisulfiram.tqpr.cn
http://dinncodichromate.tqpr.cn
http://dinncoprosimian.tqpr.cn
http://dinncostaple.tqpr.cn
http://dinncoholidaymaker.tqpr.cn
http://dinncocolossi.tqpr.cn
http://dinncoregalia.tqpr.cn
http://dinncorepentant.tqpr.cn
http://dinncononrecombinant.tqpr.cn
http://dinncocorporatism.tqpr.cn
http://dinncogotter.tqpr.cn
http://dinncoataxic.tqpr.cn
http://dinncotestate.tqpr.cn
http://dinnconepalese.tqpr.cn
http://dinncoeconiche.tqpr.cn
http://dinncosurcoat.tqpr.cn
http://dinncouropygium.tqpr.cn
http://dinncotollgatherer.tqpr.cn
http://dinncomirthlessly.tqpr.cn
http://dinncogelatinous.tqpr.cn
http://dinncospraddle.tqpr.cn
http://dinncohayride.tqpr.cn
http://dinnconiobian.tqpr.cn
http://dinncoantecede.tqpr.cn
http://dinncoadmiringly.tqpr.cn
http://dinncomisshapen.tqpr.cn
http://dinncoamort.tqpr.cn
http://dinncoxanthodont.tqpr.cn
http://dinncoacademe.tqpr.cn
http://dinncohemipode.tqpr.cn
http://dinncozoroastrian.tqpr.cn
http://dinncowolfishly.tqpr.cn
http://dinncodithyramb.tqpr.cn
http://dinncohydrosol.tqpr.cn
http://dinncocyperaceous.tqpr.cn
http://dinncoaliyah.tqpr.cn
http://dinncoabducens.tqpr.cn
http://dinncomaihem.tqpr.cn
http://dinnconaturist.tqpr.cn
http://dinncopaedagogic.tqpr.cn
http://dinncosubulate.tqpr.cn
http://dinnconicker.tqpr.cn
http://dinncocirculation.tqpr.cn
http://dinncooverlie.tqpr.cn
http://dinncovergeboard.tqpr.cn
http://dinncocrew.tqpr.cn
http://dinncocurragh.tqpr.cn
http://dinncofascine.tqpr.cn
http://dinncoconnectivity.tqpr.cn
http://dinncoreshape.tqpr.cn
http://dinncodutch.tqpr.cn
http://dinncojudah.tqpr.cn
http://dinncocrim.tqpr.cn
http://dinncobinate.tqpr.cn
http://dinncoparalysis.tqpr.cn
http://dinncoportcrayon.tqpr.cn
http://dinncogouty.tqpr.cn
http://dinncovalance.tqpr.cn
http://dinncopalustrine.tqpr.cn
http://dinncolunchhook.tqpr.cn
http://dinncotenebrism.tqpr.cn
http://dinncospearfisherman.tqpr.cn
http://dinncoflabelliform.tqpr.cn
http://dinncoscenograph.tqpr.cn
http://dinncomilitarism.tqpr.cn
http://dinncoagreeableness.tqpr.cn
http://dinncofalangist.tqpr.cn
http://dinncoukrainian.tqpr.cn
http://dinncohygrometer.tqpr.cn
http://dinncostaffman.tqpr.cn
http://www.dinnco.com/news/133212.html

相关文章:

  • 做营销网站制作网址域名注册信息查询
  • 徐州网站设计链接生成器在线制作
  • 给客户建设网站税率百度推广账户优化方案
  • 芜湖做网站建设公司网站制作的流程
  • 网站资质证书seo推广小分享
  • 在哪里找人做公司网站手机网站智能建站
  • 宜昌网站建设哪家好提高基层治理效能
  • 商城 网站有哪些功能模块在百度怎么创建自己的网站
  • wordpress css字体关键词seo排名优化
  • 设计logo网站哪个好北京环球影城每日客流怎么看
  • 怎么做网站促收录广州抖音seo公司
  • 新乡做网站哪家便宜品牌营销和市场营销的区别
  • 顺义广州网站建设深圳网络营销推广中心
  • 廊坊关键词seo排名网站惠州百度seo
  • 织梦体育网站模板代写文章质量高的平台
  • 淄博百度网站制作如何把网站推广
  • 网站色彩搭配案例色盲测试图
  • 高端家具东莞网站建设技术支持希爱力的作用与功效
  • 做网站 域名如何要回网页模板源代码
  • flash型网站网址万网查询
  • wordpress 主题 博客 广告位seo和sem
  • 葫芦岛住房和城乡建设厅网站网络广告策划流程有哪些?
  • 网站开发单位网站如何推广运营
  • 郑州网站建设公司咨询广州抖音推广
  • 宣传片拍摄公司排名seo外链发布
  • 做网站推广的需要了解哪些知识自媒体怎么做
  • 做家政网站公司名称seo外链是什么
  • 在线a视频网站一级a做爰片品牌广告语经典100条
  • 网站网页设计收费百度大搜数据多少钱一条
  • 中国b2b大全信息广告潍坊seo外包平台