您的位置:首页 > 教育 > 培训 > 运城做网站方式方法_湖南服务专业的网站制作_今天的新闻有哪些_seo资讯网

运城做网站方式方法_湖南服务专业的网站制作_今天的新闻有哪些_seo资讯网

2025/2/24 2:31:59 来源:https://blog.csdn.net/qq_63432403/article/details/142863303  浏览:    关键词:运城做网站方式方法_湖南服务专业的网站制作_今天的新闻有哪些_seo资讯网
运城做网站方式方法_湖南服务专业的网站制作_今天的新闻有哪些_seo资讯网
  • Vue2API设计是Options(配置)风格的。
  • Vue3API设计是Composition(组合)风格的。

Options类型的 API,数据、方法、计算属性等,是分散在:datamethodscomputed中的,若想新增或者修改一个需求,就需要分别修改:datamethodscomputed,不便于维护和复用。Composition 可以用函数的方式,更加优雅的组织代码,让相关功能的代码更加有序的组织在一起。

Vue2的:

<template><div class="person"><h1>Person</h1><p>Name: {{ name }}</p><p>Age: {{ age }}</p><button @click="changName">修改名字</button><button @click="changeAge">修改年龄</button><button @click="showTel">查看联系方式</button></div>
</template>
<script lang="ts">export default {name: 'Person',data() {return {name: 'John Doe',age: 25,tel: '1234567890'}},methods: {showTel() {alert(this.tel)},changName() {this.name = 'Jane Doe'},changeAge() {this.age = 30}}}
</script><style>.person {background-color: aqua;box-shadow: 0 0 10px rgba(0,0,0,0.1);}
</style>

setupVue3中一个新的配置项,值是一个函数,它是 Composition API “表演的舞台

  • setup函数返回的对象中的内容,可直接在模板中使用。
  • setup中访问thisundefined
  • setup函数会在beforeCreate之前调用,它是“领先”所有钩子执行的。

setup返回值可以是一个渲染函数:

return () => '...'

vue2和vue3共存问题:

  • Vue2 的配置(datamethos…)中可以访问到 setup中的属性、方法(通过this.访问)
  • 但在setup不能访问到Vue2的配置(datamethos…)。
  • 如果与Vue2冲突,则setup优先。
<template><div class="person"><h1>Person</h1><p>Name: {{ a }}</p><p>Age: {{ b }}</p><button @click="changName">修改名字</button><button @click="changeAge">修改年龄</button><button @click="showTel">查看联系方式</button><br><h2>{{t_name}}</h2></div>
</template>
<script lang="ts">export default {name: 'Person',data() {return {t_name:this.a,}},setup() {// 数据 -- 此时不是响应式的let name = '张三'; let age = 18;let tel = '1234567890';// 方法 -- 这样修改页面没有变化function changName() {name = '李四';}function changeAge() {age = 20;}function showTel() {alert(tel);}return {a:name, b:age, changName, changeAge, showTel}}}
</script><style>.person {background-color: aqua;box-shadow: 0 0 10px rgba(0,0,0,0.1);}
</style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com