前端面试题汇总
JavaScript
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
性能
linux
前端资料汇总
},
removeFun(){
this.$store.dispatch('removeFun')
}
}
}
#### 🌟Vuex中的核心特性
##### State
State提供唯一的公共数据源,所有共享的数据都要统一放到Store中的State中存储
###### 在组件中访问State的方式:
1).this.
s
t
o
r
e
.
s
t
a
t
e
.
全局数据名称如:
t
h
i
s
.
store.state.全局数据名称 如:this.
store.state.全局数据名称如:this.store.state.count
2).先按需导入mapState函数: import { mapState } from ‘vuex’
然后数据映射为计算属性: computed:{ …mapState([‘全局数据名称’]) }
##### Mutation
Mutation用于修改变更$store中的数据
###### 使用方式第一种
打开store.js文件,在mutations中添加代码如下
mutations: {
add(state,step){
//第一个形参永远都是state也就是$state对象
//第二个形参是调用add时传递的参数
state.count+=step;
}
}
然后在Addition.vue中给按钮添加事件代码如下:
<button @click=“Add”>+1
methods:{
Add(){
//使用commit函数调用mutations中的对应函数,
//第一个参数就是我们要调用的mutations中的函数名
//第二个参数就是传递给add函数的参数
this.$store.commit(‘add’,10)
}
}
###### 使用mutations的第二种方式:
import { mapMutations } from ‘vuex’
methods:{
…mapMutations([‘add’])
}
import { mapState,mapMutations } from ‘vuex’
export default {
data() {
return {}
},
methods:{
//获得mapMutations映射的sub函数
…mapMutations([‘sub’]),
//当点击按钮时触发Sub函数
Sub(){
//调用sub函数完成对数据的操作
this.sub(10);
}
},
computed:{
…mapState([‘count’])
}
}
##### Action
在mutations中不能编写异步的代码,会导致vue调试器的显示出错。
在vuex中我们可以使用Action来执行异步操作。
###### 操作步骤第一种
打开store.js文件,修改Action,如下:
actions: {
addAsync(context,step){
setTimeout(()=>{
context.commit(‘add’,step);
},2000)
}
}
然后在Addition.vue中给按钮添加事件代码如下
<button @click=“AddAsync”>…+1
methods:{
AddAsync(){
this.$store.dispatch(‘addAsync’,5)
}
}
###### 操作步骤第二种
import { mapActions } from ‘vuex’
methods:{
…mapMutations([‘subAsync’])
}
import { mapState,mapMutations,mapActions } from ‘vuex’
export default {
data() {
return {}
},
methods:{
//获得mapMutations映射的sub函数
…mapMutations([‘sub’]),
//当点击按钮时触发Sub函数
Sub(){
//调用sub函数完成对数据的操作
this.sub(10);
},
//获得mapActions映射的addAsync函数
…mapActions([‘subAsync’]),
asyncSub(){
this.subAsync(5);
}
},
computed:{
…mapState([‘count’])
}
}
##### Getter
Getter用于对Store中的数据进行加工处理形成新的数据
它只会包装Store中保存的数据,并不会修改Store中保存的数据,当Store中的数据发生变化时,Getter生成的内容也会随之变化
###### 使用
打开store.js文件,添加getters,然后打开Addition.vue中,添加插值表达式使用getters
export default new Vuex.Store({
…
getters:{
//添加了一个showNum的属性
showNum : state =>{
return ‘最新的count值为:’+state.count;
}
}
})
或者也可以在Addition.vue中,导入mapGetters,并将之映射为计算属性
import { mapGetters } from ‘vuex’
computed:{
…mapGetters([‘showNum’])
}
#### 🌟写在最后
下方是小编开设的公众号,每天为大家推送前端硬核知识!期待您的加入哦!
![在这里插入图片描述](https://img-blog.csdnimg.cn/52c3d4ed13474200a466d88a6057e49e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Yeg5L2V5b-D5YeJ,size_11,color_FFFFFF,t_70,g_se,x_16)
>
> ✨
>
>
>
>
>
> 原
>
>
> 创
>
>
> 不
>
>
> 易
>
>
> ,
>
>
> 大
>
>
> 佬
>
>
> 们
>
>
> 给
>
>
> 个
>
>
> 支
>
>
> 持
>
>
> !
>
>
> !
>
>
> !
>
>
>
>
> \textcolor{blue}{原创不易,大佬们给个支持!!!}
>
>
> 原创不易,大佬们给个支持!!!
>
> 👍
>
>
>
>
>
> 点
>
>
> 赞
>
>
> ,
>
>
> 你
>
>
> 的
>
>
> 认
>
>
> 可
>
>
> 是
>
>
> 我
>
>
> 创
>
>
> 作
>
>
> 的
>
>
> 动
>
>
> 力
>
>
> !
>
>
>
>
> \textcolor{green}{点赞,你的认可是我创作的动力!}
>
>
> 点赞,你的认可是我创作的动力!
>
> ⭐️
>
>
>
>
>
> 收
>
>
> 藏
>
>
> ,
>
>
> 你
>
>
> 的
>
>
> 青
>
>
> 睐
>
>
> 是
>
>
> 我
>
>
> 努
>
>
> 力
>
>
> 的
>
**前端面试题汇总**
![](https://img-blog.csdnimg.cn/img_convert/8087f8c06b129975b3b7be228aa41f0d.png)
**JavaScript**
![](https://img-blog.csdnimg.cn/img_convert/7796de226b373d068d8f5bef31e668ce.png)
**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**
**性能**
![](https://img-blog.csdnimg.cn/img_convert/d7f6750332c78eb27cc606540cdce3b4.png)
**linux**
![](https://img-blog.csdnimg.cn/img_convert/ed368cc25284edda453a4c6cb49916ef.png)
**前端资料汇总**
![](https://img-blog.csdnimg.cn/img_convert/6e0ba223f65e063db5b1b4b6aa26129a.png)