Vuex

Vuex中的各个js文件的用途
一. vue中各个组件之间传值
1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);
2.非父子组件或父子组件
通过数据总数Bus,this.root.root. root.root.emit(‘事件名’,参数1,参数2,…)
3.非父子组件或父子组件
更好的方式是在vue中使用vuex
方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据
二. Vuex
官方解释:
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),
让其在各个页面上实现数据的共享包括状态,并且可操作
Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
变量传值的演变形式

在这里插入图片描述
图解Vuex各组件

在这里插入图片描述
官方图解Vuex
在这里插入图片描述
三. vuex使用步骤
1 安装

npm install vuex -S

在这里插入图片描述
2 在src目录里创建store模块,分别维护state/actions/mutations/getters
在这里插入图片描述
3 在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块

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)
const store = new Vuex.Store({
  state,
  getters,
  actions,
  mutations
 })
export default store
  1. vuex的核心概念:store、state、getters、mutations、actions
    store
    每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。
const store = new Vuex.Store({
   state,    // 共同维护的一个状态,state里面可以是很多个全局状态
   getters,  // 获取数据并渲染
   actions,  // 数据的异步操作
   mutations  // 处理数据的唯一途径,state的改变或赋值只能在这里
  })

在router里的index.js加入相关的路由

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import AppMain from '@/components/AppMain'
import Articles from '@/views/sys/Articles'
import VuexPage1 from '@/views/sys/VuexPage1'
import VuexPage2 from '@/views/sys/VuexPage2'
import Login from '@/views/Login'
import Reg from '@/views/Reg'
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: '/sys/Articles',
     name: 'Articles',
     component: Articles
    },
    {
     path: '/sys/VuexPage1',
     name: 'VuexPage1',
     component: VuexPage1
    },
    {
     path: '/sys/VuexPage2',
     name: 'VuexPage2',
     component: VuexPage2
    }
   ]
  }
  ]
})

利用vuex取值
1 state(保存数据的容器)状态,即要全局读写的数据
state.js

export default {
    resturantName:'小酒馆'
}

VuexPage1.vue

<template>
 <div>
  <ul>
   <li>这是第一张页面:{{msg}}</li>
  </ul>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    
   };
  },
  computed:{
   msg(){
    //不推荐 vuex遵循了编码解耦的 各模块各司其职
    return this.$store.state.resturantName;
   }
  }
 }
</script>
<style>
</style>

在这里插入图片描述
this.$store.state.resturantName;//不建议
2 getters(getXxx) 获取数据并渲染
getters.js

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

VuexPage2.vue

<template>
 <div>
  <ul>
   <li>这是第二张页面:{{msg}}</li>
  </ul>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    
   };
  },
  computed:{
   msg(){
    return this.$store.getters.getResturantName;
   }
  }
 }
</script>
<style>
</style>

在这里插入图片描述

注1:getters将state中定义的值暴露在this. s t o r e . g e t t e r s 对 象 中 , 我 们 可 以 通 过 如 下 代 码 访 问 t h i s . store.getters对象中,我们可以通过如下代码访问this. store.getters访this.store.getters.resturantName

注2:state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态,如下:

 computed: {
        resturantName: function() {
          return this.$store.getters.resturantName;
        }
      }

利用vuex存值
处理数据的唯一途径,state的改变或赋值只能在这里
mutations.js

export default {
    // type(事件类型): 其值为setResturantName
    // payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
  setResturantName: (state, payload) => {
      state.resturantName = payload.resturantName;
      }
  }

VuexPage.js

<template>
 <div>
  <ul>
   <li>这是第一张页面:{{msg}}</li>
   <li>
    <input v-model="newName" />
    <button @click="buy">先买</button>
   </li>
  </ul>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    
   };
  },
  computed:{
   msg(){
    //不推荐 vuex遵循了编码解耦的 各模块各司其职
    return this.$store.state.resturantName;
   }
  },
  methods:{
   buy(){ //同步
    this.$store.commit('setResturantName',{
     resturantName:this.newName
    });
   }
  }
 }
</script>
<style>
</style>

在这里插入图片描述

this.$store.state.resturantName;//不建议
2 getters(getXxx) 获取数据并渲染
getters.js

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

VuexPage2.vue

<template>
 <div>
  <ul>
   <li>这是第二张页面:{{msg}}</li>
  </ul>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    
   };
  },
  computed:{
   msg(){
    return this.$store.getters.getResturantName;
   }
  }
 }
</script>
<style>
</style>

在这里插入图片描述

注1:getters将state中定义的值暴露在this. s t o r e . g e t t e r s 对 象 中 , 我 们 可 以 通 过 如 下 代 码 访 问 t h i s . store.getters对象中,我们可以通过如下代码访问this. store.getters访this.store.getters.resturantName

注2:state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态,如下:

  computed: {
        resturantName: function() {
          return this.$store.getters.resturantName;
        }
      }

利用vuex存值
处理数据的唯一途径,state的改变或赋值只能在这里
mutations.js

export default {
    // type(事件类型): 其值为setResturantName
    // payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
  setResturantName: (state, payload) => {
      state.resturantName = payload.resturantName;
      }
  }

VuexPage.js

<template>
 <div>
  <ul>
   <li>这是第一张页面:{{msg}}</li>
   <li>
    <input v-model="newName" />
    <button @click="buy">先买</button>
   </li>
  </ul>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    
   };
  },
  computed:{
   msg(){
    //不推荐 vuex遵循了编码解耦的 各模块各司其职
    return this.$store.state.resturantName;
   }
  },
  methods:{
   buy(){ //同步
    this.$store.commit('setResturantName',{
     resturantName:this.newName
    });
   }
  }
 }
</script>
<style>
</style>

在这里插入图片描述

注1:mutations中方法的调用方式
不能直接调用this.store.mutations.setResturantName(′KFC′),必须使用如下方式调用:this.store.mutations.setResturantName(′KFC′),必须使用如下方式调用:this. store.mutations.setResturantName(‘KFC’),必须使用如下方式调用:this.store.mutations.setResturantName(′KFC′),必须使用如下方式调用:this.store.commit(type,payload);

 // 1、把载荷和type分开提交
       store.commit('setResturantName',{
         resturantName:'KFC'
       })
       // 2、载荷和type写到一起
      store.commit({
        type: 'setResturantName',
        resturantName: 'KFC'
      })

注2:一定要记住,Mutation 必须是同步函数。为什么呢?异步方法,我们不知道什么时候状态会发生改变,所以也就无法追踪了
如果我们需要异步操作,Mutations就不能满足我们需求了,这时候我们就需要Actions了

  mutations: {
        someMutation (state) {
          api.callAsyncMethod(() => {
            state.count++
          })
        }
       }

Vuex的异步加载问题
Action类似于 mutation,不同在于:
1.Action提交的是mutation,而不是直接变更状态
2.Action可以包含任意异步操作
3.Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性
但是他们并不是同一个实例,context 包含:

  1. state、2. rootState、3. getters、4. mutations、5. actions 五个属性
    所以在这里可以使用 context.commit 来提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。
    actions.js
export default{
 setResturantNamePlus:(context,payload)=>{
  console.log('x');
    setTimeout(()=>{
  console.log('y');
  //调用同步函数
  context.commit('setResturantName',{
   resturantName:payload.resturantName
  })
 },6000);
 console.log('z');
 }
}

VuexPage1.vue

<template>
 <div>
  <ul>
   <li>这是第一张页面:{{msg}}</li>
   <li>
    <input v-model="newName" />
    <button @click="buy">先买</button>
    <button @click="dobuy">阴谋</button>
   </li>
  </ul>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    
   };
  },
  computed:{
   msg(){
    //不推荐 vuex遵循了编码解耦的 各模块各司其职
    return this.$store.state.resturantName;
   }
  },
  methods:{
   buy(){//同步
    this.$store.commit('setResturantName',{
     resturantName:this.newName
    });
   },
   dobuy(){//异步
    this.$store.dispatch('setResturantNamePlus',{
    resturantName:this.newName
   })
   }
  }
 }
</script>
<style>
</style>

注1:actions中方法的调用方式语法如下:

this.$store.dispatch(type,payload);

例如:

this.$store.dispatch('setResturantNameByAsync',{resturantName: '啃德鸡2'});

注2:action中提交mutation

  context.commit('setResturantName',{resturantName: '啃德鸡2'});

注3:VUEX 的 actions 中无法获取到 this 对象
如果要在actions 或者 mutations 中使用this对象。可以在调用的时候把this对象传过去
{resturantName: ‘啃德鸡2’,_this:this}//this就是在调用时的vue实例

调用Ajax
action.js

export default{
 setResturantNamePlus:(context,payload)=>{
  console.log('x');
    setTimeout(()=>{
  console.log('y');
  //调用同步函数
  context.commit('setResturantName',{
   resturantName:payload.resturantName
  })
 },6000);
 console.log('z');
 },
 doAjax:(context,payload)=>{
  let _this = payload._this;
  let url = _this.axios.urls.SYSTEM_MENU_TREE;
  _this.axios.post(url, {}).then((response) => {
   console.log(response);
  }).catch(function(error) {
   console.log(error);
  });
 }
}

VuexPage1.vue

<template>
 <div>
  <ul>
   <li>这是第一张页面:{{msg}}</li>
   <li>
    <input v-model="newName" />
    <button @click="buy">先买</button>
    <button @click="dobuy">阴谋</button>
    <button @click="doajax">调用ajax</button>
   </li>
  </ul>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    
   };
  },
  computed:{
   msg(){
    //不推荐 vuex遵循了编码解耦的 各模块各司其职
    return this.$store.state.resturantName;
   }
  },
  methods:{
   buy(){//同步
    this.$store.commit('setResturantName',{
     resturantName:this.newName
    });
   },
   dobuy(){//异步
    this.$store.dispatch('setResturantNamePlus',{
    resturantName:this.newName
   })
   },
   doajax() {
    this.$store.dispatch('doAjax', {
     _this: this
    })
            }
  }
 }
</script>
<style>
</style>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值