栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

后台搭建——SpringBoot+Vue学习(八)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

后台搭建——SpringBoot+Vue学习(八)

Vue使用路由

通过改变 URL,在不重新请求页面的情况下,更新页面视图。

1、把home分成不同的部分,登录、用户、管理等界面。在router-index.js中修改并在侧边栏导入。这里一定要加上Vue.use(VueRouter)

2、写出Aside组件,记得把collapse等值传进去,不然会没有显示。
Aside的代码:







3、在router-index.js中添加路由守卫代码,其中的next必须写,否则路由不出来。

router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称,为了在Header组件中去使用
  store.commit("setPath")  // 触发store的数据更新
  next()  // 放行路由
})

4、在header中用这种方式进行引用:

 computed: {
     currentPathName () {
       return this.$store.state.currentPathName;  //需要监听的数据
     }
   },
   watch: {
     currentPathName (newVal, oldVal) {
       console.log(newVal)
     }
   },

使用:

// 使用
  
      首页
      {{ currentPathName }}
  

5、安装vuex:
npm i vuex -S
6、建立store.js来使用vuex:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        currentPathName: ''
    },
    mutations: {
        setPath (state) {
            state.currentPathName = localStorage.getItem("currentPathName")
        }
    }
})

export default store

7、在main.js中引入vuex

import store from './store'

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号