码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • vue3引入pinna配置使用


    引入pinna

    npm install pinia -S
    
    • 1

    在src文件里面创建store文件-index.ts 在main.ts中引用pinna

    import {createPinia} from 'pinia'
    const store = createPinia()
    createApp(App).use(router).use(store).mount('#app')
    
    • 1
    • 2
    • 3

    index.ts

    index.ts文件

    defineStore定义容器
    参数1:是对仓库的命名,名称必须具备唯一性;
    参数2:配置的选项对象,即state、getters、actions,
    其中state的写法必须是函数,为了避免在服务端交叉请求导致的状态数据污染,
    而且必须是箭头函数,为了更好的TS类型推导。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    import { defineStore } from 'pinia'// 引入
    
    export const useStore = defineStore("main", {
       state: () => {
          return {
             count: 10,
             name: 'wl',
             arr: [1, 2, 3],
          }
       },
       //类似于computed 可以帮我们去修饰我们的值
       getters: {
          //  函数接受一个可选参数 state
          count10(state) { return state.count + 10 }
       },
       //可以操作异步 和 同步提交state
       actions: {
          //不要使用箭头函数定义action,因为箭头函数绑定外部this
          changeState(num: any, str: any) {   //不要使用箭头函数定义action,因为箭头函数绑定外部this
             this.count += num
             this.name += str
             this.arr.push(5)
          }
          // this.$patch({})或this.$patch(state=>{})    //还可通过$patch修改state的数据
       }
    })
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    其它页使用

    其他页面使用

    <template>
      <div>
        <span>数量:{{ test.count }}---{{ count }}</span>
        <br>
        <span>姓名:{{ test.name }}---{{ name }}</span>
        <br>
        <span>arr:{{ test.arr }}---{{ arr }}</span>
      </div>
    </template>
    <script setup lang="ts">
    import { useStore } from "./store/index"//引入store文件
    import { storeToRefs } from 'pinia'//引入storeToRefs
    import { toRefs } from "vue";
    const test = useStore()//声明赋值引入的store文件
    const { count, name, arr, count10 } = storeToRefs(test)//数据解构出来
    //使用storeToRefs函数将state里的数据解构出来实现响应式
    
    // 3种方法 一种通过test.自定义名称进行调用
    // 另一种是修改多个数据,用$patch 
    // 另一种是直接通过state
    // test.$patch({
    //   count: test.count,
    //   name: test.name,
    //   arr: [...test.arr, 4]
    // })
    
    //  test.$patch(state => {
    //  state.count++
    //  state.name += '~~'
    //  state.arr.push(5)
    //})
    
    test.changeState(10,'hello') //逻辑比较多用action
    </script>
    
    <style scoped></style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    参考

  • 相关阅读:
    404 not found nginx(dist打包后,刷新和跳转都是404 not found nginx的问题) 解决方案(打包发布在服务器)
    凉鞋的 Godot 笔记 001. 概述
    java家教信息计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    景联文科技:深度探究自动驾驶重要方向——车路协同
    GO微服务实战第十九节 如何保障分布式系统的高可用性?
    跳槽前恶补面试题,成功上岸阿里,拿到33k的测开offer
    spring 如何解决循环依赖
    重装系统以后怎么还原电脑原本的资料
    一篇了解Java Web中的数据库开发的JDBC基础
    速腾聚创发布全固态补盲激光雷达E1,成立合资公司,备战百万产能
  • 原文地址:https://blog.csdn.net/m0_53912016/article/details/133775973
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号