前端:Vue3学习-2

前端:Vue3学习-2

    • [1. vue3 新特性-defineOptions](#1. vue3 新特性-defineOptions)
    • [2. vue3 新特性-defineModel](#2. vue3 新特性-defineModel)
    • [3. vue3 Pinia-状态管理工具](#3. vue3 Pinia-状态管理工具)
    • [4. Pinia 持久化插件 -> pinia-plugin-persistedstate](#4. Pinia 持久化插件 -> pinia-plugin-persistedstate)

1. vue3 新特性-defineOptions

如果要定义组件的name或其他自定义的属性,还是得回归原始得方法----再添加一个普通得script标签,这样就会存在两个script标签。

html 复制代码
<script>
export default{
	name:'组件名'
}
</script>
<script setup>
</script>
<template>
</template>

因此在vue3.3中新引入了defineOptions宏,这样上述就可以这样写了。

html 复制代码
<script setup>
defineOptions({
	name:'组件名'
})
</script>

2. vue3 新特性-defineModel

在vue3中,自定义组件上使用v-model,相当于传递一个modelValue属性,同时触发update:modelValue事件。即

html 复制代码
<Child v-model="isVisible">
// 相当于
<Child :modelValue="isVisible" @update:modelValue="isVisble=$event">

还需要在子组件中定义props和emits,如下

父组件

html 复制代码
<script setup>
import sonCom from '@/components/son-com.vue'
import { ref } from 'vue';

const _str = ref('');

</script>
<template>
  <div>
    <sonCom v-model="_str"></sonCom>
    <p>{{ _str }}</p>
  </div>
</template>

子组件

html 复制代码
<script setup>
const props = defineProps({
    modelValue:String   
})

const emits = defineEmits(['update:modelValue'])
const inp = (event)=>{
    emits('update:modelValue',event.target.value)
}
</script>

<template>
    <div>
        <input type="text" :value="modelValue" @input="inp">
    </div>
</template>

运行结果:

现在使用defineModel,子组件代码可以修改如下:

html 复制代码
<script setup>
import { defineModel } from 'vue'
const modelValue = defineModel()
const inp = (event)=>{
    modelValue.value = event.target.value
}
</script>

<template>
    <div>
        <input type="text" :value="modelValue" @input="inp">
    </div>
</template>

需要注意的是,如果modelValue在结构代码中使用时,不需要".value",直接如下:

html 复制代码
<input type="text" :value="modelValue" @input="e=>modelValue=e.target.value">

需要在vite.config.js进行配置,上述才能生效,如下:

js 复制代码
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue({
      script:{
        defineModel:true
      }
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

3. vue3 Pinia-状态管理工具

Pinia时vue的最新状态管理工具,是vuex的替代品。

优势如下:

  1. 提供了更加简明的api,去掉了mutation;
  2. 提供符合组合式风格的api
  3. 去掉了modules的概念,每一个store都是一个独立的模块;

pinia的安装命令为:

js 复制代码
npm install pinia

使用,首先需要在main.js文件中导入pinia,然后创建pinia实例,进行pinia注册。

js 复制代码
import { createApp } from 'vue'
import App from './App.vue'

import { createPinia } from 'pinia'
// 导包
const pinia = createPinia()
// 创建pinia实例

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

新建store js文件,用于初始化和操作数据。

js 复制代码
import { defineStore } from "pinia"
import { ref } from "vue"
// counter作为store的唯一标识
export const useStore = defineStore('counter',()=>{
    const count = ref(0)
    // 数据state count
    const increment = ()=>{
        count.value ++
    }

    return{
        count, increment
    }
})

使用上述数据,直接在对应组件中导入,然后使用即可,如下:

html 复制代码
<script setup>
import { useStore } from '@/store/counter'

const counter = useStore()
</script>

<template>
    <div>
        <p>子组件</p>
        <p>{{ counter.count }}</p>
        <button @click="counter.increment">+1</button>
    </div>
</template>

使用storeToRefs对store进行解构
由于store 是一个用reactive 包裹的对象,reactive包裹的对象只有当为对象时,才是响应式的,因此如果直接进行解构数据,那么所结构出的数据不具有响应式 ,此时解构数据,需要用到storeToRefs,方法直接解构即可。

html 复制代码
<script setup>
import { useStore } from '@/store/counter'
import {storeToRefs} from 'pinia'
const counter = useStore()
const { count } = storeToRefs(counter)
const { increment } = counter
</script>

<template>
    <div>
        <p>子组件</p>
        <p>{{ count }}</p>
        <button @click="increment">+1</button>
    </div>
</template>

4. Pinia 持久化插件 -> pinia-plugin-persistedstate

安装命令

js 复制代码
npm install pinia-plugin-persistedstate

js 复制代码
yarn add pinia-plugin-persistedstate

在mian.js文件添加插件配置

js 复制代码
import { createApp } from 'vue'
import App from './App.vue'

import { createPinia } from 'pinia'
// 导包
import persist from 'pinia-plugin-persistedstate'

const pinia = createPinia()
// 创建pinia实例
pinia.use(persist)
createApp(App).use(pinia).mount('#app')

在需要持久化的模块添加如下配置即可

js 复制代码
import { defineStore } from "pinia"
import { ref } from "vue"
// counter作为store的唯一标识
export const useStore = defineStore('counter',()=>{
    const count = ref(0)
    // 数据state count
    const increment = ()=>{
        count.value ++
    }

    return{
        count, increment
        }
    }, 
    {
        persist: true
    // 开启当前模块的持久化
    }
)

运行结果:

只是在本地浏览器进行存储,具体存储在localstorage里边。

相关推荐
diygwcom几秒前
electron-updater实现electron全量版本更新
前端·javascript·electron
Hello-Mr.Wang17 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
程序员凡尘44 分钟前
完美解决 Array 方法 (map/filter/reduce) 不按预期工作 的正确解决方法,亲测有效!!!
前端·javascript·vue.js
潮汐退涨月冷风霜1 小时前
机器学习之非监督学习(四)K-means 聚类算法
学习·算法·机器学习
GoppViper1 小时前
golang学习笔记29——golang 中如何将 GitHub 最新提交的版本设置为 v1.0.0
笔记·git·后端·学习·golang·github·源代码管理
羊小猪~~1 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
Charles Ray2 小时前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
我要吐泡泡了哦3 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
骑鱼过海的猫1233 小时前
【tomcat】tomcat学习笔记
笔记·学习·tomcat
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql