IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> VueX之五个属性值&&同步存取值&&异步问题 -> 正文阅读

[JavaScript知识库]VueX之五个属性值&&同步存取值&&异步问题

课程目标?

1、了解vuex中的各个js文件的用途

2、利用vuex同步存值

3、利用vuex取值

4、Vuex的异步加载问题及后台调用问题

1.官方解释

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库)?让其在各个页面上实现数据的共享包括状态,并且可操作。

一、Vuex中的各个js文件的用途

变量传值的演变形式

???方法1:?用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。

? ?方法2:?我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据

??图解Vuex各组件

???Vuex分成五个部分:

???1.State:单一状态树

???2.Getters:状态获取

???3.Mutations:触发同步事件

???4.Actions:提交mutation,可以包含异步操作

???5.Module:将vuex进行分模块

?官方图解Vuex

二、vuex使用步骤

1.安装??? ??npm install vuex -S

?2.在src下创建store模块,分别维护state/actions/mutations/getters

?3.store/index.js文件中新建vuexstore实例,并注册上面引入的各大模块

import Vue from 'vue'
import Vuex from 'vuex'
import state from './State'
import getters from './Getters'
import actions from './Actions'
import mutations from './Mutations'
Vue.use(Vuex)
/*      每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。 */
const store = new Vuex.Store({
 	state, /* 共同维护的一个状态,state里面可以是很多个全局状态 */
 	getters,   /* 获取数据并渲染 */
 	actions,   /* 数据的异步操作 */
 	mutations /*   处理数据的唯一途径,state的改变或赋值只能在这里 */
 })
 
 export default store
 

?4?main.js中导入并使用store实例

?store:?每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态?(state)

?三、vuex的传值问题?

1.先按照数据库路径建立两个vue

2.State.js

export default {
        resturantName:'飞歌餐馆'
}

3.VuexPage1.vue

<template>
  <div>
    <h3>  页面1:欢迎来到{{msg}}</h3>
 
   <button @click="pata">盘塔</button>
  </div>
 
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
 
    }
  },
  methods:{
    pata(){
 
    }
  },
  computed:{
    msg(){
      return this.$store.state.resturantName;
    }
  }
}
</script>
 
<style>
</style>

.VuexPage2.vue

<template>
  <div>
    <h3>页面2:欢迎来到{{msg}}
</h3>
 
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
 
    }
  },
  computed:{
    msg(){
      return this.$store.state.resturantName;
    }
  }
}
</script>
 
<style>
</style>

两个界面的msg是相同的区别在于分别显示页面一和页面二

4.配置路由

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/views/Login'
import Reg from '@/views/Reg'
import AppMain from '@/components/AppMain'
import LeftNav from '@/components/LeftNav'
import TopNav from '@/components/TopNav'
import Articles from '@/views/sys/Articles'
import VuexPage1 from '@/views/sys/VuexPage1'
import VuexPage2 from '@/views/sys/VuexPage2'

Vue.use(Router)

export default new Router({
? routes: [{
? ? ? path: '/',
? ? ? name: 'Login',
? ? ? component: Login
? ? },
? ? {
? ? ? path: '/Login',
? ? ? name: 'Login',
? ? ? component: Login
? ? },
? ? {
? ? ? path: '/Reg',
? ? ? name: 'Reg',
? ? ? component: Reg
? ? },
? ? {
? ? ? path: '/AppMain',
? ? ? name: 'AppMain',
? ? ? component: AppMain,
? ? ? children: [{
? ? ? ? path: '/LeftNav',
? ? ? ? name: 'LeftNav',
? ? ? ? component: LeftNav
? ? ? }, {
? ? ? ? path: '/TopNav',
? ? ? ? name: 'TopNav',
? ? ? ? component: TopNav
? ? ? }, {
? ? ? ? path: '/sys/Articles',
? ? ? ? name: 'Articles',
? ? ? ? component: Articles
? ? ? },
? ? ? {
? ? ? ? path: '/sys/VuexPage1',
? ? ? ? name: 'VuexPage1',
? ? ? ? component: VuexPage1
? ? ? },
? ? ? {
? ? ? ? path: '/sys/VuexPage2',
? ? ? ? name: 'VuexPage2',
? ? ? ? component: VuexPage2
? ? ? }

? ? ? ]
? ? }

? ]
})
?

结果:

? this.$store.state.resturantName;//不建议使用

? ?于是推荐以下这个方法来取值

4.1getters(getXxx)

4.1.getters.js

export default {
getResturantName:(state) => {
         return state.resturantName;
       }
}

4.2?VuexPage1.vue改变的代码

<script>
export default {
? name: 'HelloWorld',
? data () {
? ? return {
? ? }
? },
? methods:{
? ? pata(){
this.$store.commit("setResturantName",{
? resturantName:"天之道足浴"
})
? ? }
? },
? computed:{
? ? msg(){
? ? ?// return this.$store.state.resturantName;
? ? ?return this.$store.getters.getResturantName;
? ? }
? }
}
</script>

?四.Vuex存值

1.Mutations.js

/* 操作变量,相当于set方法 */
export default{
  ??????// payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
setResturantName:(state,payload)=>{
  state.resturantName=payload.resturantName;
}
}

2.VuexPage1.vue改变的代码

<script>
export default {
? name: 'HelloWorld',
? data () {
? ? return {
? ? }
? },
? methods:{
? ? pata(){
this.$store.commit("setResturantName",{
? resturantName:"我的世界"

})
? ? }
? },
? computed:{
? ? msg(){
? ? ? return this.$store.state.resturantName;
? ? }
? }
}
</script>

?3.点击盘塔的运行结果:

五、Vuex的异步?

1.Action.js

/* Mutations的升级版,mutations操作变量。是同步操作,action是异步操作变量 */
export default {

setResturantNameAsync: (context, payload) => {
  //context等价于this.$store,也就是它代表了Vuex的上下文
  //在这个文件中是可以调用同步文件mutations.js定义的同步方法
setTimeout(function() {
  context.commit('setResturantName', payload); //Action提交的是mutation

}, 3000);
			// state.resturantName = payload.resturantName;


	}
}

2.VuexPage1.vue

<template>
? <div>
? ? <h3> ?页面1:欢迎来到{{msg}}</h3>

? ?<button @click="pata">盘塔</button>
? ?<button @click="pataAsync">最终</button>
? </div>

</template>

<script>
export default {
? name: 'HelloWorld',
? data () {
? ? return {
? ? }
? },
? methods:{
? ? pata(){
this.$store.commit("setResturantName",{
? resturantName:"我的世界"
})
? ? },
pataAsync(){
? this.$store.dispatch("setResturantNameAsync",{
? ? resturantName:"我的界"
? })
? }

? ? ? },

? computed:{
? ? msg(){
? ? ?// return this.$store.state.resturantName;
? ? ?return this.$store.getters.getResturantName;
? ? }
? }
}
</script>

<style>
</style>

?3.点击最终运行结果:

?

?六、文件中与后台服务器做数据交互?

1.Mutations.js

/* 操作变量,相当于set方法 */
export default {
       /*    payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器 */
 setResturantName:(state, payload)=>{
  state.resturantName=payload.resturantName;
},
doAjax: (context, payload) => {
  //需求:想在当前的文件中与后台服务器做数据交互

  let url = this.axios.urls.SYSTEM_MENU_TREE;
     this.axios.post(url,{}).then((resp) => {
              console.log(resp);
  this.menus=resp.data.result;
              }).catch(function(error) {
              console.log(error)
            });

}
}

2.vuexpage1.vue

<template>
? <div>
? ? <h3> ?页面1:欢迎来到{{msg}}</h3>

? ?<button @click="pata">盘塔</button>
? ?<button @click="pataAsync">最终</button>
? ?<button @click="doAjax">vuex与后台交互</button>
? </div>

</template>

<script>
export default {
? name: 'HelloWorld',
? data () {
? ? return {
? ? }
? },
? methods:{
? ? pata(){
this.$store.commit("setResturantName",{
? resturantName:"我的世界"
})
? ? },
pataAsync(){
? this.$store.dispatch("setResturantNameAsync",{
? ? resturantName:"我的界"
? })
? },
doAjax(){
? this.$store.commit("doAjax",{
? })
? }

? ? ? },

? computed:{
? ? msg(){
? ? ?// return this.$store.state.resturantName;
? ? ?return this.$store.getters.getResturantName;
? ? }
? }
}
</script>

<style>
</style>

?4.点击ajax与后台交互则会报错:

5.解决

?1.vuexpage1.vue

doAjax(){
? this.$store.commit("doAjax",{
_this:this
? })
? }

??2.Mutations.js

export default {
? ? ? ?/* ? ?payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器 */
?setResturantName:(state, payload)=>{
? state.resturantName=payload.resturantName;
},
doAjax: (context, payload) => {
? //需求:想在当前的文件中与后台服务器做数据交互
?let _this=payload._this;
? let url = _this.axios.urls.SYSTEM_MENU_TREE;
? ? ?_this.axios.post(url,{}).then((resp) => {
? ? ? ? ? ? ? console.log(resp);
? ? ? ? ? ? ? }).catch(function(error) {
? ? ? ? ? ? ? console.log(error)
? ? ? ? ? ? });

}
}

3.运行

?

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-11-16 18:44:30  更:2021-11-16 18:44:50 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 22:15:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码