Vue3+Ts+Vite2+Pinia 搭建开发框架

一、使用vite快速创建脚手架

1. 使用yarn运行安装命令:yarn create vite

2. 选择vue vue-ts 完成安装

3. 进入vue3_ts_vite_pinia项目,使用yarn命令安装依赖,依赖安装完成后,使用yarn dev启动项目

二、路由配置(vue-router@4)

1. 使用yarn安装vue-router@4: yarn add vue-router@4

2. src文件夹下新建router文件夹,router文件夹下新建index.ts

import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [   //注:RouteRecordRaw为内置类型
    {
        path: '/',
        name: 'Index',
        component: () => import('@/pages/index/Index.vue'),
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes,
})

export default router
 

3. 在main.ts中,引入router并注册

import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'
const app = createApp(App)
app.use(router)
app.mount('#app')

4. 在App.vue中设置路由展现出口 <router-view></router-view>

<template>
    // <img alt="Vue logo" src="./assets/logo.png" />
    // <HelloWorld msg="Hello Vue 3 + TypeScript + Vite" />
    <router-view></router-view>
</template>

 

三、状态管理(Pinia配置)

Pinia 是 Vue.js 的轻量级状态管理库,也是Vue核心团队推荐的状态管理库,由于Pinia也是Vuex研发团队的产品,以及尤雨溪的加持,极大可能会替代Vuex,即使pinia的推广不太顺利也并不用过多担心,其许多使用方式很有可能会移植到Vuex5中。
相较于Vuex,Pinia上手更简单,mutations,并且actions支持同步或异步。

1. 使用yarn安装 pinia@next:yarn add pinia@next

2. src文件夹下新建store文件夹,store文件夹下新建main.ts

import { defineStore } from 'pinia' export const useUserStore = defineStore({ id: 'user', state: () => ({ name: '用户名' }), getters: { nameLength: (state) => state.name.length, }, actions: { updataUser(data: any) { console.log(data) } } })

3. 在main.ts中,引入createPinia

import { createApp } from 'vue' import App from './App.vue' import router from './router/index' import { createPinia } from 'pinia' const app = createApp(App) app.use(createPinia()) app.use(router) app.mount('#app')

四、基本使用
1. 获取state
  • 直接获取

<template>
    <div>{ {userStore.name}}</div>
</template>
<script setup lang="ts">
  import { useUserStore } from "@/store/user.ts"
  const userStore = useUserStore()

</script>
 

  • computed获取

<template>
    <div>{ {name}}</div>
</template>
<script setup lang="ts">
import { useUserStore } from "@/store/user.ts"
const userStore = useUserStore()
const name=computed(()=>{
    userStore.name
})
</script>

  • 结构获取,但会失去响应式,需要使用storeToRefs

<template>
    <div>{ {name}}</div>
</template>
<script setup lang="ts">
import { useUserStore } from "@/store/user.ts"
import {storeToRefs} from 'pinia'
const userStore = useUserStore()
const name=storeToRefs(userStore)
</script>

2. 设置state

  • 直接修改
你好!对于使用 Vue 3、TypeScript 和 Vite搭建项目,并结合 Pinia 进行状态管理,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了 Node.js 和 npm(或者使用 yarn)。 2. 创建一个新的项目文件夹,并进入该文件夹。 3. 在终端中运行以下命令来初始化一个新的 Vue 3 项目: ``` npm init vite@latest ``` 在初始化过程中,选择使用 Vue 3、TypeScript 和默认配置。 4. 进入项目文件夹并安装依赖: ``` cd <project-folder> npm install ``` 5. 接下来,安装 Pinia: ``` npm install pinia ``` 6. 在 `src` 目录下创建一个 `store` 文件夹,并在其中创建名为 `index.ts` 的文件。 7. 在 `index.ts` 中编写你的 Pinia store。例如,你可以创建一个名为 `counter` 的 store,并且在其中定义一个状态和一些操作: ```typescript import { defineStore } from &#39;pinia&#39;; export const useCounterStore = defineStore(&#39;counter&#39;, { state: () => ({ count: 0, }), actions: { increment() { this.count++; }, decrement() { this.count--; }, }, }); ``` 8. 在应用的入口文件 `main.ts` 中导入并使用 Pinia: ```typescript import { createApp } from &#39;vue&#39;; import { createPinia } from &#39;pinia&#39;; import App from &#39;./App.vue&#39;; const app = createApp(App); const pinia = createPinia(); app.use(pinia); app.mount(&#39;#app&#39;); ``` 9. 在组件中使用 Pinia store。在你的 Vue 组件中,可以使用 `useStore` 函数来访问 Pinia store: ```typescript import { useCounterStore } from &#39;../store&#39;; export default { setup() { const counterStore = useCounterStore(); return { counterStore, }; }, }; ``` 10. 最后,你可以在组件中使用 `counterStore` 来访问状态和操作: ```vue <template> <div> <p>{{ counterStore.count }}</p> <button @click="counterStore.increment()">Increment</button> <button @click="counterStore.decrement()">Decrement</button> </div> </template> <script> import { useCounterStore } from &#39;../store&#39;; export default { setup() { const counterStore = useCounterStore(); return { counterStore, }; }, }; </script> ``` 这样,你就可以使用 Vue 3、TypeScript、VitePinia 搭建一个基本的项目并进行状态管理了。希望对你有帮助!如果还有其他问题,请随时问我。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TonyH2002

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值