一.defineOptions(Vue3.3)
背景:
有<script setup>之前,如果要定义props,emits可以轻而易举地添加一个与setup平级的属性
但用了<script setup>后,就没法这么干了setup属性已经没有了,自然无法添加与其平级的属性
为了解决这一问题,引入了defineProps 与defineEmits 这两个宏。但这只解决了props与emits这
**两个属性,**如果我们要定义组件的name或其他自定义的属性,还是得回到最原始的用法---再添加
一个普通的<script>标签。这样就会存在两个<script>标签。让人无法接受。引入了defineOptions
宏主要是用来定义Options APl的选项 。可以用defineOptions 定义任意的选项,props, emits,
expose, slots 除外(因为这些可以使用 defineXXX来做到)
在views/login文件夹下创建index.vue文件里面是会报错的,我们通过引入defineOptions来解决这
一问题,如下:
javascript
<script setup>
defineOptions({
name: 'LoginIndex'
})
</script>
<template>
<div>
登录页
</div>
</template>
二.defineModel(Vue3.4)
从Vue 3.4开始,推荐的实现方式是使用defineModel()宏
父组件可以用v-model绑定一个值:
javascript
<script setup>
import InputItem from './components/inputItem.vue'
import { ref } from 'vue'
const msg = ref('Hello World!')
</script>
<template>
<h1>{{ msg }}</h1>
<InputItem v-model="msg" />
</template>
defineModel() 返回的值是一个ref。它可以像其他ref一样被访问以及修改,不过它能起到在父组件
和当前变量之间的双向绑定的作用:
1.它的.value和父组件的v-model的值同步;
2.当它被子组件变更了,会触发父组件绑定的值一起更新
这意味着你也可以用v-model把这个ref绑定到一个原生input元素上,在提供相同的v-model用法的
同时轻松包装原生input元素
子组件:
javascript
<script setup>
const model = defineModel()
</script>
<template>
<span>My input: </span>
<input v-model="model">
</template>

底层机制:

补充v-model的参数
组件上的v-model也可以接受一个参数:
javascript
<script setup>
import InputItem from './components/inputItem.vue'
import { ref } from 'vue'
const bookTitle = ref('v-model argument example')
</script>
<template>
<h1>{{ bookTitle }}</h1>
<InputItem v-model:title="bookTitle" />
</template>
在子组件中,我们可以通过将字符串作为第一个参数传递给defineModel()来支持相应的参数:
javascript
<script setup>
const title = defineModel('title')
</script>
<template>
<span>My input: </span>
<input v-model="title">
</template>

三.Pinia
什么是Pinia
Pinia是Vue的最新状态管理工具,是Vuex的替代品
1.提供更加简单的API (去掉了mutation)
2.提供符合,组合式风格的API (和Vue3新语法统一)
3.去掉了modules的概念,每一个store都是一个独立的模块
4.配合TypeScript更加友好,提供可靠的类型推断
添加Pinia
创建一个新的项目:npm create vue@latest

安装一下项目依赖:npm install
启动项目:npm run dev
安装Pinia:npm install pinia @vue/devtools-api
在main.js中创建Pinia实例
javascript
import { createApp } from "vue";
import { createPinia } from "pinia"; // 引入pinia
import App from "./App.vue";
const pinia = createPinia(); // 创建pinia实例
const app = createApp(App); // 创建根实例
app.use(pinia); // 挂载pinia实例到根实例
app.mount("#app");
定义store
在src文件夹下新创建文件store,在store中创建仓库,每一个仓库都是独立的
提供数据,使用数据
javascript
import { defineStore } from "pinia";
import { ref } from "vue";
// 定义store
// defineStore(仓库的唯一标识, () => {} )
export const useCounterStore = defineStore("counter", () => {
// 1.声明数据 state
const count = ref(0);
const msg = ref("hello pinia");
// 2.声明操作数据的方法 action
// 3.声明基于数据派生的计算属性 getters
return {
count,
msg,
};
});
App.vue中:
javascript
<script setup>
import sonCom1 from './components/son1.vue'
import sonCom2 from './components/son2.vue'
import { useCounterStore } from './store/counter'
const counterStore = useCounterStore()
</script>
<template>
<h2>根组件 - {{ counterStore.count }} - {{ counterStore.msg }}</h2>
<sonCom1 />
<sonCom2 />
</template>

操作数据的方法
javascript
import { defineStore } from "pinia";
import { ref } from "vue";
// 定义store
// defineStore(仓库的唯一标识, () => {} )
export const useCounterStore = defineStore("counter", () => {
// 1.声明数据 state
const count = ref(0);
const msg = ref("hello pinia");
// 2.声明操作数据的方法 action
const addCount = () => count.value++;
const subCount = () => count.value--;
// 3.声明基于数据派生的计算属性 getters
return {
count,
msg,
addCount,
subCount,
};
});
子组件中:
javascript
<script setup>
defineOptions({
name: 'sonCom1'
})
import { useCounterStore } from '../store/counter'
const counterStore = useCounterStore()
</script>
<template>
<div>子组件1 - {{ counterStore.count }} <button @click="counterStore.addCount">+</button></div>
</template>
计算属性getters(computed)
javascript
import { defineStore } from "pinia";
import { ref, computed } from "vue";
// 定义store
// defineStore(仓库的唯一标识, () => {} )
export const useCounterStore = defineStore("counter", () => {
// 1.声明数据 state
const count = ref(0);
const msg = ref("hello pinia");
// 2.声明操作数据的方法 action
const addCount = () => count.value++;
const subCount = () => count.value--;
// 3.声明基于数据派生的计算属性 getters(computed)
const doubleCount = computed(() => count.value * 2);
return {
count,
msg,
addCount,
subCount,
doubleCount,
};
});
action异步
异步action函数的写法和组件中获取异步数据的写法完全一致


javascript
<button @click="channelStore.getList">获取数据</button>
<ul>
<li v-for="item in channelStore.channelList" :key="item.id">
{{ item.name }}
</li>
</ul>
storeToRefs方法
javascript
//直接解构不处理,数据会丢失响应式
const { count , msg } = counterStore
javascript
<script setup>
import sonCom1 from './components/son1.vue'
import sonCom2 from './components/son2.vue'
import { useCounterStore } from './store/counter'
import { storeToRefs } from 'pinia';
const counterStore = useCounterStore()
const { count, msg } = storeToRefs(counterStore);
</script>
<template>
<h2>根组件 - {{ count }} - {{ msg }}</h2>
<sonCom1 />
<sonCom2 />
</template>
使用storeToRefs方法可以将结构出的数据实现响应式,注意:方法是可以直接解构的
持久化
Pinia持久化插件网站:Pinia Plugin Persistedstate
1.安装插件:npm i pinia-plugin-persistedstate
2.将插件添加到你的pinia实例中:
3.在声明store时将新persist选项设置为true

