Vue3+Vite+Ts配置路由、Pinia 配置和简单介绍、配置 SCSS

# 安装
npm install vue-router@4
<!-- src\views\home\index.vue -->
<template>
  <div>
    首页
  </div>
</template>

<script setup lang="ts">
</script>

<style scoped>
</style>

<!-- src\views\home\index.vue -->
<template>
  <div>
    首页
  </div>
</template>

<script setup lang="ts">
</script>

<style scoped>
</style>

<!-- src\App.vue -->
<template>
  <router-view />
</template>

<script setup lang="ts">
</script>

// src\router\index.ts
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes:RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/home/index.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  }
]

const router = createRouter({
  // history: createWebHashHistory(), // hash 路由模式
  history: createWebHistory(), // history 路由模式
  routes // 路由规则
})

export default router

// src\main.ts
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

createApp(App)
  .use(router)
  .mount('#app')

集成 Pinia

集成 Pinia
// src\main.ts
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'

createApp(App)
  .use(router)
  .use(createPinia())
  .mount('#app')

定义 Store

Pinia 使用 defineStore() 定义 store,它返回一个用于创建 store 实例的函数:

defineStore() 需要一个唯一的 name(也称为 id) 作为第一个参数传递(必填)
defineStore() 约定使用 use... 作为函数名(如 useCartStore)
通过 defineStore() 定义的 store 相当于 Vuex 中的 Module。

// src\store\index.ts
import { defineStore } from 'pinia'

const useStore = defineStore('main', {
  state: () => ({
    count: 0
  }),
  getters: {
    doubleCount(state) {
      return state.count * 2
    }
  },
  actions: {
    increment() {
      this.count++
    }
  }
})

export default useStore

使用

store 一旦被实例化,就可以直接访问 stategetters 和 actions

Pinia 没有 Vuex 的 Mutations,可以在 Actions 中直接使用同步和异步方法。

<!-- src\views\home\index.vue -->
<template>
  <div>
    首页
    <button @click="store.increment">
      {{ store.count }} => {{ store.doubleCount }}
    </button>
  </div>
</template>

<script setup lang="ts">
import useStore from '@/store'

const store = useStore()
</script>

使用解构语法

请注意,store 是一个用 reactive 包装的对象,这意味着访问 state 和 getters 时不需要写 .value,就像 setup 中的 props,我们也不能将其直接解构:

<!-- src\views\home\index.vue -->
<template>
  <div>
    首页
    <button @click="store.increment">
      {{ store.count }} => {{ store.doubleCount }}
    </button>

    <div>count 永远是 0:{{ count }}</div>
    <div>doubleCount 永远是 0:{{ doubleCount }}</div>
  </div>
</template>

<script setup lang="ts">
import useStore from '@/store'

const store = useStore()

const { count, doubleCount } = store
// 在 script-setup 中使用解构赋值要通过 defineExpose 暴漏出去
defineExpose({
  count
})
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值