一文看懂vue3中setup()和 <script setup><script>的区别

本文探讨Vue3中的setup()函数与<script setup>的区别,包括组件不再需要components注册、props和emit的语法变化,以及如何通过setup()的两个参数props和context来接收和处理数据。同时,文章指出在setup()中定义的数据和方法需return返回,并介绍了不使用props配置时props接收值的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

setup ()

setup () 是vue3新增加的组件。vue3采用了组合式 API ,为了使用组合式API,我们需要一个入口,在vue3组件中,称之为setup。

(简单点来说,就是vue2里面的data,method,computed···全不要啦,所有数据方法全写在setup里)

来看一个简单的例子:

<template>
  <div class="box">
    <h1 style="text-align: center;">{
  {testData}}</h1>
  </div>
</template>

<script>
export default {
  setup () {
    const testData = 'hello world'
  return {
    testData
  }
  }
}
</script>

注意:在setup()中定义的数据,方法都需要 return 返回出去,不然会报错

再看一个调用方法的例子:

<template>
  <div class="box" style="margin: 0 auto;text-align: center;">
    <button @click="testFunciton">点我</button>
  </div>
</template>

<script>
export default {
  setup () {
    const testFunciton = function () {
      console.log('你点了我');
    }
  return {
    testFunciton
  }
  }
}
</script>

如何双向绑定数据请阅读官网的ref,reactive,torefs 这里不详细讲解vue3的变化 (双向绑定,生命周期等)

setup () 函数应该有两个参数,props和 contenxt 参数

第一个参数props:

props是一个对象,包含父组件传递给子组件的所有数据。

在子组件中使用props进行接收。包含配置声明并传

Pinia是一个用于Vue.js 3的状态管理库,它可以帮助我们更好地组织管理应用程序的状态。在Vue.js 3中使用Pinia进行模块化状态管理有几个步骤。 首先,我们需要在项目中安装Pinia库。可以通过npm或yarn来安装Pinia。 ``` npm install pinia ``` 或 ``` yarn add pinia ``` 接下来,我们需要创建一个或多个store文件来定义我们的模块。每个store文件对应一个模块。例如,我们可以创建一个`userStore.ts`文件来管理用户相关的数据,创建一个`systemStore.ts`文件来管理系统相关的数据。 在`userStore.ts`中,我们可以定义一个`UserStore`类,该类使用Pinia的`defineStore`函数来定义模块的状态相关的操作。在这个类中,我们可以定义我们需要的状态属性方法。 ```typescript import { defineStore } from &#39;pinia&#39; export const userStore = defineStore({ id: &#39;user&#39;, state: () => ({ name: &#39;&#39;, age: 0, email: &#39;&#39; }), actions: { setName(name: string) { this.name = name }, setAge(age: number) { this.age = age }, setEmail(email: string) { this.email = email } } }) ``` 在`systemStore.ts`中,我们可以按照同样的方式定义系统相关的状态操作。 接下来,我们需要创建一个根store来整合所有的模块。我们可以创建一个`store.ts`文件,使用Pinia的`createPinia`函数来创建一个根store实例。 ```typescript import { createPinia } from &#39;pinia&#39; export const pinia = createPinia() ``` 最后,在我们的Vue应用程序的入口文件中,我们需要将Pinia插件添加到Vue应用程序中。 ```typescript import { createApp } from &#39;vue&#39; import App from &#39;./App.vue&#39; import { pinia } from &#39;./store&#39; const app = createApp(App) app.use(pinia) app.mount(&#39;#app&#39;) ``` 现在,我们就可以在我们的组件中使用Pinia的`useStore`函数来获取我们定义的各个模块的实例,并使用它们的状态操作。 ```typescript import { useStore } from &#39;pinia&#39; export default { setup() { const userStore = useStore(&#39;user&#39;) // 使用userStore的状态操作 userStore.setName(&#39;John&#39;) console.log(userStore.name) return { userStore } } } ``` 通过这种方式,我们可以将应用程序的状态按照模块进行组织管理,使得代码更加清晰可维护。每个模块都有自己的状态操作,最后通过根store进行整合。这样可以更好地实现模块化管理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Vue3 - Pinia 模块化(详细教程)](https://blog.youkuaiyun.com/weixin_44198965/article/details/128114009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [【一文搞定Pinia】超详细的Pinia教程,比Vuex香太多了!](https://blog.youkuaiyun.com/weixin_42373175/article/details/128268962)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值