網站首頁 Vue 正文
簡介
說明
本文介紹Vue的插件:Vuex。包括:優缺點、使用場景、示例。
官網
優缺點
優點
1.響應式
屬于 vue 生態一環,,能夠觸發響應式的渲染頁面更新。 (localStorage 就不會)
2.可預測的方式改變數據
避免數據污染
3.無需轉換數據
JS 原生的數據對象寫法(不需要做轉換)。(localStorage 需要做轉換)
缺點
1.復雜
適合大應用,不適合小型應用
2.不能持久化(刷新頁面后vuex中的state會變為初始狀態)
解決方案
結合localStorage
vuex-persistedstate插件
使用場景
當我們多個頁面需要共享數據時就可以使用Vuex。
實際開發中我們一般在下面這種情況使用它:
當前登錄用戶的信息
購物車的信息
收藏的信息
用戶的地理位置
示例
本處用計數器來測試:一個組件修改計數器的值,其余兩個不相關組件可以監測到時計數器值的改變。
做法:一個組件(ComponentA)將數據共享給另外兩個不相關組件(ComponentB和ComponentC),外部用Parent.vue放置這三個組件。
安裝Vuex并引入
?1.安裝vuex
在工程目錄下執行:npm install vuex
2.編寫vuex的store
創建目錄store,在其下邊創建CounterStore.js,內容如下:?
import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const couterStore = new Vuex.Store( { state: { count1: 0, count2: 0, }, mutations: { increment1(state) { state.count1++; }, decrement1(state) { state.count1--; }, increment2: state => state.count2++, decrement2: state => state.count2--, } } ); export default couterStore;
3.main.js引入CounterStore.js
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import CouterStore from './store/CounterStore' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, store: CouterStore, components: { App }, template: '<App/>' })
按照JS語法,key與value重名時可以簡寫:(很多教程這么寫)
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import store from './store/CounterStore' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, store, components: { App }, template: '<App/>' })
業務代碼
代碼
ComponentA.vue
<template> <div class="container"> <h3>ComponentA</h3> <button @click="increment1">增加:第1個計數器</button> <button @click="decrement1">減少:第1個計數器</button><br><br> <button @click="increment2">增加:第2個計數器</button> <button @click="decrement2">減少:第2個計數器</button> </div> </template> <script> export default { data() { return { count1: 0, count2: 0, } }, methods:{ increment1() { this.$store.commit('increment1') }, decrement1() { this.$store.commit('decrement1') }, increment2() { this.$store.commit('increment2') }, decrement2() { this.$store.commit('decrement2') } } } </script> <style scoped> .container { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
ComponentB.vue
<template> <div class="container"> <h3>ComponentB</h3> 計數器的值:{{msg}} <!--也可以這么寫:--> <!--計數器的值:{{this.$store.state.count1}}--> </div> </template> <script> export default { computed:{ msg() { return this.$store.state.count1; } } } </script> <style scoped> .container { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
ComponentC.vue
<template> <div class="container"> <h3>ComponentC</h3> 計數器的值:{{msg}} <!--也可以這么寫:--> <!--計數器的值:{{this.$store.state.count2}}--> </div> </template> <script> export default { computed:{ msg() { return this.$store.state.count2; } } } </script> <style scoped> .container { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
Parent.vue
<template> <div class="outer"> <h3>父組件</h3> <component-a></component-a> <component-b></component-b> <component-c></component-c> </div> </template> <script> import ComponentA from "./ComponentA"; import ComponentB from "./ComponentB"; import ComponentC from "./ComponentC"; export default { name: 'Parent', components: {ComponentA, ComponentB, ComponentC}, data() { return { name: 'Tony', age: 20, phoneNumber: '1234567890' } } } </script> <style scoped> .outer { margin: 20px; border: 2px solid red; padding: 20px; } </style>
路由
import Vue from 'vue' import Router from 'vue-router' import Parent from "../components/Parent"; Vue.use(Router) export default new Router({ routes: [ { path: '/parent', name: 'Parent', component: Parent, } ], })
測試
訪問: http://localhost:8080/#/parent
原文鏈接:https://blog.csdn.net/feiying0canglang/article/details/122631509
相關推薦
- 2022-09-18 Pandas?Query方法使用深度總結_python
- 2023-07-18 SpringBoot中線程池初始化,并且可配置線程池參數
- 2022-05-22 nginx常用配置conf的示例代碼詳解_nginx
- 2022-06-08 freertos實時操作系統臨界段保護開關中斷及進入退出_操作系統
- 2022-07-02 C語言由淺入深理解指針_C 語言
- 2022-09-03 C#實現工廠方法模式_C#教程
- 2022-10-25 Git中bundle命令的使用詳解_相關技巧
- 2023-02-09 Python關鍵字?asynico基本用法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支