if (this. r o u t e . p a t h . t o L o w e r C a s e ( ) = = = ′ / o p i n i o n ′ ∣ ∣ t h i s . route.path.toLowerCase() === '/opinion' || this. route.path.toLowerCase()===′/opinion′∣∣this.route.path.toLowerCase() === ‘/Opinion’) {
_self. p a r e n t . parent. parent.parent.back2()
} else if(this.$route.path === ‘/account_center’ ){
this.$router.push(‘/main’)
} else {
this.$router.go(-1) // 返回上一层
}
},
问题二:主页里面有显示座位号的地方,之前是通过localstorage获取的登录信息,但是修改了个人中心的座位号以后希望所有座位号的地方都修改,我改成了请求接口的形式,但是座位号没变,不知道是哪里出问题了。
这个请求接口好像无法渲染到页面
main.vue
upgrade.vue
upgradeAlert.vue
解:
可以看到最初的座位号我用的父子组件传值,这一层一层的特别繁琐,后面遇到从接口获取的数据竟然更新不到页面上,一直是空就很烦恼
后来才下定决心学习了vuex,而Vuex就是解决这种很繁琐的页面传值问题
这里修改成vuex获取座位号以后遇到的问题:
呼唤铃一开始的座位号是空,刷新后也是空
原因
vuex存储在内存,localstorage(本地存储)则以文件的方式存储在本地,永久保存;sessionstorage( 会话存储 ) ,临时保存。
当刷新页面(这里的刷新页面指的是 --> F5刷新,属于清除内存了)时vuex存储的值会丢失,sessionstorage页面关闭后就清除掉了,localstorage不会。
源: VueX和localstorage的区别、VueX的应用场景
介于这个灵感,我有在Main.vue里面添加了这个
created() {
this.$store.state.seat = JSON.parse(localStorage.getItem(‘seat’))
},
destroyed() {
this.$store.commit(‘seat’, JSON.parse(localStorage.getItem(‘seat’)));
},
改好的效果图:(即便页面刷新也不会出现空了)
讲一下思路:
只用vuex的话会存在刷新就清空state的数据,这里就让它在每一次提交座位号的时候让它同时存到localstorage里面,问题就解了
提交了两次,一次是在登录完成跳转主页之前
另一次是在个人中心修改的时候,
还有就是要注意,在mutation方法里面我也给他存了一次,这样每一次调用的时候都能保证它在更新数据
这里登录输入的座位号
我在login.vue的跳转成功之前把它存入了localStorage,提交了store
localStorage.setItem(‘seat’, JSON.stringify(this.loginForm.seat))
this.$store.commit(‘newSeat’, this.loginForm.seat)
在store/index.js给它初始化,写了方法让它可以在其他页面调用修改
任何一个页面都可以通过this.$store.state.seat
或者this.$store.getters.getSeat
获取座位号
const state = {
seat: JSON.parse(localStorage.getItem(‘seat’))
}
const getters = {
getSeat(state) {
return state.seat
}
}
const mutations = {
// mutations放改变state的初始值的方法
// 调用方法this.$store.comit(‘newSeat’,2a)
newSeat(state, seat) {
state.seat = seat
localStorage.setItem(‘seat’, JSON.stringify(seat))
}
}
Reseat.vue
<input id=“seat” type=“text” v-model=“seat” ref=“seat” οnkeyup="value=value.replace(/[\W]/g,‘’) ">
computed: {
seat: {
get() {
return this.$route.query.seat
},
set(newVal) {
console.log(newVal)
this.$store.commit(‘newSeat’, newVal)
}
}
}
this.$route.query.seat
是在前一个页面跳转时候直接路由传值,其实这里应该可以直接this.$store.state.seat
获取(没有测试)
rename () {
this.$router.push({ path: ‘/rename’, query: { ‘id’: this.id, ‘name’: this.name } })
},
其他的没做改动
JavaScript 和 ES6
在这个过程你会发现,有很多 JS 知识点你并不能更好的理解为什么这么设计,以及这样设计的好处是什么,这就逼着让你去学习这单个知识点的来龙去脉,去哪学?第一,书籍,我知道你不喜欢看,我最近通过刷大厂面试题整理了一份前端核心知识笔记,比较书籍更精简,一句废话都没有,这份笔记也让我通过跳槽从8k涨成20k。