获取两个input框求和结果:state=>{{ getStateSum }} getters=>{{getters.getSum}}
// 安装 vue/cl
npm install -g @vue/cli
// 快速构建 my-project 项目
vue create my-project
node环境要求
Vue CLI 4.x 需要 Node.js v8.9 或更高版本 (推荐 v10 以上)
vue2项目直接安装: npm i vue-router默认安装最新版本,不兼容vue2,推荐指定特定版本安装
npm i vue-router@3.5.2
npm i vuex@3
element-ui
iView
Ant Design
Boostrap
...
Vantui Mintui NuTUI ...
npm init vue@latest
npm init vue@3
npm init vue@2或者
npm create vue@3
npm create vue@2
npm install vue-router@4
或者
yarn add vue-router@4
注意:如果 SEO 对你很重要,你应该使用 createWebHistory
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'const router = createRouter({history: createWebHistory(),// history: createWebHashHistory(),routes: [{path: '/',name: 'home',component: () => import('../views/HomeView.vue')},{path: '/about',name: 'about',component: () => import('../views/AboutView.vue')}]
})export default router
State
Getter
Mutation
Action
Module
npm install vuex@next --save 或者 yarn add vuex@next --save
export default { state () {return {count: 0}},mutations: {// 负责修改state中的count值countMutations (state, newVal) {state.count = newVal}},actions: {countActions ({ commit }, params) {// 触发mutations中的countMutations函数并传递参数commit("countMutations", params)}}
}
import { createStore } from 'vuex'
import moduleTest from './modules/moduleTest'// 创建一个新的 store 实例
export default createStore({state () {return {sum: 0}},mutations: {// 负责修改state中的count值sumMutations (state, newVal) {state.sum += newVal}},actions: {sumActions ({ commit }, params) {// 触发mutations中的countMutations函数并传递参数setTimeout(()=>{commit("sumMutations", params)},300)}},getters: {getSum: state => state.sum},modules: {moduleTest}
})
import { createApp } from "vue";
import App from "./App.vue";
import store from "./store";createApp(App).use(store).mount("#app");
state: 展示count的值: {{getStateCount}}getters: 展示count的值: {{getters.count}}
获取两个input框求和结果:state=>{{ getStateSum }} getters=>{{getters.getSum}}
dev-tools 支持
跟踪动作、突变的时间线
Store 出现在使用它们的组件中
time travel 和 更容易的调试
热模块更换
在不重新加载页面的情况下修改您的 Store
在开发时保持任何现有状态
插件:使用插件扩展 Pinia 功能
为 JS 用户提供适当的 TypeScript 支持或 autocompletion
服务器端渲染支持
与 Vuex 相比,Pinia 提供了一个更简单的 API,具有更少的规范,提供了 Composition-API 风格的 API,最重要的是,在与 TypeScript 一起使用时具有可靠的类型推断支持。此外,pinia已经实现vuex@5大部分功能,因此pinia可以被称为vuex@5。
一是去掉 mutations,它的存在显得流程过于复杂和冗余
二是不再需要注入、导入函数、调用函数、享受自动完成功能!
三是无需动态添加 Store,默认情况下它们都是动态的
四是不再有 modules 的嵌套结构
五是 没有 命名空间模块。鉴于 Store 的扁平架构,“命名空间” Store 是其定义方式所固有的,您可以说所有 Store 都是命名空间的。
yarn add pinia
# 或者使用 npm
npm install pinia
import { createApp } from 'vue'
import { createPinia } from 'pinia'import App from './App.vue'
import router from './router'const app = createApp(App)app.use(createPinia())
app.use(router)app.mount('#app')
函数方式一
import { ref, computed } from 'vue';
import { defineStore } from 'pinia';const useCounterStore = defineStore('counter', () => {// state定义状态变量countconst count = ref(0);// getters中获取计算结果 doubleCountconst doubleCount = computed(() => count.value * 2);// actions直接操作state状态变量修改function increment() {count.value++;}return { count, doubleCount, increment };
});
对象方式二
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter',{// state定义状态变量countstate: () => ({ count: 0 }),// getters中获取计算结果 doubleCountgetters: {doubleCount: state => state.count * 2},// actions直接操作state状态变量修改actions: {increment() {this.count++;}}
});
展示count: {{counterStore.count}}
展示getters计算结果: {{counterStore.doubleCount}}
修改count
下一篇:AI稳定生成图工业链路打造