当前位置: 首页 > news >正文

免费信息网站建设谷歌seo查询

免费信息网站建设,谷歌seo查询,网站 做购物车,昆明网站建设在 Vue.js 中,vue-router 是官方的路由管理器,它允许你通过不同的 URL 显示不同的组件,从而实现单页面应用(SPA)。以下是关于 vue-router 的详细介绍,包括路由配置、动态导入、嵌套路由、路由跳转、导航菜单…

在 Vue.js 中,vue-router 是官方的路由管理器,它允许你通过不同的 URL 显示不同的组件,从而实现单页面应用(SPA)。以下是关于 vue-router 的详细介绍,包括路由配置、动态导入、嵌套路由、路由跳转、导航菜单、动态路由、重置路由、页面刷新和动态菜单等内容。

1. 路由配置

安装 vue-router

如果你还没有安装 vue-router,可以通过 npm 或 yarn 安装:

npm install vue-router

或者

yarn add vue-router
基本配置

在 Vue 项目中,通常会在一个单独的文件(如 router/index.js)中配置路由。

// router/index.js
import Vue from 'vue';
import Router from 'vue-router';
import Home from '../components/Home.vue';
import About from '../components/About.vue';Vue.use(Router);export default new Router({mode: 'history', // 使用历史模式routes: [{path: '/',name: 'Home',component: Home},{path: '/about',name: 'About',component: About}]
});

main.js 中引入并使用路由配置:

import Vue from 'vue';
import App from './App.vue';
import router from './router';new Vue({router,render: h => h(App)
}).$mount('#app');

2. 动态导入

为了优化应用的加载速度,可以使用动态导入(import())来实现代码分割。

const Home = () => import('../components/Home.vue');
const About = () => import('../components/About.vue');export default new Router({routes: [{path: '/',name: 'Home',component: Home},{path: '/about',name: 'About',component: About}]
});

3. 默认路由

默认路由通常用于重定向用户到主页或其他默认页面。可以通过 redirect 属性来实现。

404 页面用于处理未匹配到任何路由的情况。可以通过一个通配符路由(*)来捕获所有未匹配的路径。

import Vue from 'vue';
import Router from 'vue-router';
import Home from '../components/Home.vue';
import NotFound from '../components/NotFound.vue';Vue.use(Router);export default new Router({mode: 'history',routes: [{path: '/',name: 'Home',component: Home},{path: '/about',name: 'About',component: () => import('../components/About.vue')},{path: '/user/:id',name: 'User',component: () => import('../components/User.vue')},{path: '*',name: 'NotFound',component: NotFound},{path: '/default',redirect: '/'}]
});

4. 嵌套路由

嵌套路由允许你在组件内部嵌套子路由,非常适合实现多级菜单或复杂的页面结构。

const Home = () => import('../components/Home.vue');
const About = () => import('../components/About.vue');
const Profile = () => import('../components/Profile.vue');
const Settings = () => import('../components/Settings.vue');export default new Router({routes: [{path: '/',name: 'Home',component: Home},{path: '/about',component: About,children: [{path: 'profile',component: Profile},{path: 'settings',component: Settings}]}]
});

在父组件中使用 <router-view> 来显示子路由的内容:

<template><div><h1>About</h1><router-view></router-view></div>
</template>

5. 路由跳转

可以通过 <router-link> 或编程式导航来实现路由跳转。

使用 <router-link>
<template><div><h1>Home</h1><router-link to="/about/profile">Go to Profile</router-link></div>
</template>
编程式导航
export default {methods: {goToProfile() {this.$router.push('/about/profile');}}
};

6. 动态路由

从后端获取路由信息,并动态地将这些信息传入 vue-router。这种方式特别适用于需要根据后端配置动态生成路由的场景,例如基于角色的路由权限控制或动态菜单生成。

1. 后端返回的路由数据

假设后端返回的路由数据是一个 JSON 格式,包含路径、名称和组件路径。例如:

[{"path": "/admin","name": "Admin","componentPath": "components/AdminComponent.vue"},{"path": "/user","name": "User","componentPath": "components/UserComponent.vue"}
]
2. 动态加载组件

在 Vue 中,可以通过 import() 动态加载组件。为了方便管理,可以将组件路径存储为字符串,并在需要时动态加载。

创建动态组件加载方法

src/utils 文件夹中创建一个 dynamicImport.js 文件,用于动态加载组件。

// src/utils/dynamicImport.js
export default function dynamicImport(componentPath) {return () => import(`@/${componentPath}`);
}
3. 获取后端数据并动态添加路由

在主组件(如 App.vue)中,使用 Axios 获取后端数据,并动态添加路由。

App.vue
<template><div id="app"><nav><router-link to="/">主页</router-link> |<router-link v-for="route in dynamicRoutes" :key="route.name" :to="route.path">{{ route.name }}</router-link></nav><router-view /></div>
</template><script>
import axios from 'axios';
import dynamicImport from '@/utils/dynamicImport';export default {name: 'App',data() {return {dynamicRoutes: []};},created() {this.fetchRoutes();},methods: {async fetchRoutes() {try {const response = await axios.get('https://your-backend-api.com/routes');const routes = response.data;// 清空现有动态路由this.dynamicRoutes = [];// 移除所有动态添加的路由this.$router.options.routes.forEach(route => {if (route.name !== 'Home') {this.$router.removeRoute(route.name);}});// 动态添加路由routes.forEach(route => {const component = dynamicImport(route.componentPath);this.$router.addRoute({path: route.path,name: route.name,component});this.dynamicRoutes.push(route);});} catch (error) {console.error('获取路由信息失败:', error);}}}
};
</script>
4. 配置路由

router/index.js 中,初始化路由配置。由于动态路由是在组件中添加的,因此初始路由配置可以只包含默认路由。

// router/index.js
import Vue from 'vue';
import Router from 'vue-router';
import Home from '../components/Home.vue';Vue.use(Router);export default new Router({mode: 'history',routes: [{path: '/',name: 'Home',component: Home}]
});
总结

通过使用字符串拼接的方式动态加载组件,你可以根据后端返回的路由配置动态生成路由。这种方法特别适用于需要根据用户角色或权限动态生成路由的场景,例如多租户系统或角色管理系统。通过动态加载组件,可以实现代码分割,优化应用的加载速度。

7.页面刷新

使用 localStoragesessionStorage 来存储用户角色或权限信息,并根据这些信息动态生成路由,是一种常见的解决方案。这种方式可以避免每次页面刷新或用户切换角色时重新从后端获取路由信息,从而提高应用的性能和用户体验。

1. 使用 localStorage 或 sessionStorage
区别
  • localStorage:数据存储在浏览器中,直到手动清除,即使关闭浏览器后数据仍然存在。

  • sessionStorage:数据存储在浏览器会话中,关闭浏览器标签或窗口后数据会被清除。

对于用户角色或权限信息,通常使用 localStorage,因为这些信息在用户登录后需要持久化,直到用户主动登出。

2. 示例:使用 localStorage 动态生成路由
后端返回的路由数据

假设后端返回的路由数据是一个 JSON 格式,包含路径、名称和组件路径。例如:

[{"path": "/admin","name": "Admin","componentPath": "components/AdminComponent.vue"},{"path": "/user","name": "User","componentPath": "components/UserComponent.vue"}
]
动态组件加载方法

src/utils 文件夹中创建一个 dynamicImport.js 文件,用于动态加载组件。

// src/utils/dynamicImport.js
export default function dynamicImport(componentPath) {return () => import(`@/${componentPath}`);
}
获取后端数据并动态添加路由

在主组件(如 App.vue)中,使用 Axios 获取后端数据,并动态添加路由。同时,将路由信息存储到 localStorage 中。

<template><div id="app"><nav><router-link to="/">主页</router-link> |<router-link v-for="route in dynamicRoutes" :key="route.name" :to="route.path">{{ route.name }}</router-link></nav><router-view /></div>
</template><script>
import axios from 'axios';
import dynamicImport from '@/utils/dynamicImport';export default {name: 'App',data() {return {dynamicRoutes: []};},created() {this.fetchRoutes();},methods: {async fetchRoutes() {try {// 尝试从 localStorage 获取路由信息let routes = JSON.parse(localStorage.getItem('userRoutes'));if (!routes) {// 如果没有,从后端获取const response = await axios.get('https://your-backend-api.com/routes');routes = response.data;// 将路由信息存储到 localStoragelocalStorage.setItem('userRoutes', JSON.stringify(routes));}// 清空现有动态路由this.dynamicRoutes = [];// 移除所有动态添加的路由this.$router.options.routes.forEach(route => {if (route.name !== 'Home') {this.$router.removeRoute(route.name);}});// 动态添加路由routes.forEach(route => {const component = dynamicImport(route.componentPath);this.$router.addRoute({path: route.path,name: route.name,component});this.dynamicRoutes.push(route);});} catch (error) {console.error('获取路由信息失败:', error);}}}
};
</script>
配置路由

router/index.js 中,初始化路由配置。由于动态路由是在组件中添加的,因此初始路由配置可以只包含默认路由。

// router/index.js
import Vue from 'vue';
import Router from 'vue-router';
import Home from '../components/Home.vue';Vue.use(Router);export default new Router({mode: 'history',routes: [{path: '/',name: 'Home',component: Home}]
});
3. 用户切换角色或重新登录

在用户切换角色或重新登录时,需要清除 localStorage 中的路由信息,并重新获取路由配置。

methods: {async fetchRoutes() {try {// 尝试从 localStorage 获取路由信息let routes = JSON.parse(localStorage.getItem('userRoutes'));if (!routes) {// 如果没有,从后端获取const response = await axios.get('https://your-backend-api.com/routes');routes = response.data;// 将路由信息存储到 localStoragelocalStorage.setItem('userRoutes', JSON.stringify(routes));}// 清空现有动态路由this.dynamicRoutes = [];// 移除所有动态添加的路由this.$router.options.routes.forEach(route => {if (route.name !== 'Home') {this.$router.removeRoute(route.name);}});// 动态添加路由routes.forEach(route => {const component = dynamicImport(route.componentPath);this.$router.addRoute({path: route.path,name: route.name,component});this.dynamicRoutes.push(route);});} catch (error) {console.error('获取路由信息失败:', error);}},handleUserChange() {// 用户切换角色或重新登录时调用// 清除 localStorage 中的路由信息localStorage.removeItem('userRoutes');this.fetchRoutes();}
}


文章转载自:
http://skint.fwrr.cn
http://ammoniation.fwrr.cn
http://tallow.fwrr.cn
http://refulgence.fwrr.cn
http://verligte.fwrr.cn
http://ammonoid.fwrr.cn
http://polytonal.fwrr.cn
http://scagliola.fwrr.cn
http://blindness.fwrr.cn
http://cariogenic.fwrr.cn
http://faineant.fwrr.cn
http://hireable.fwrr.cn
http://piazza.fwrr.cn
http://forcipiform.fwrr.cn
http://ph.fwrr.cn
http://regrade.fwrr.cn
http://springhouse.fwrr.cn
http://thunderation.fwrr.cn
http://leadswinger.fwrr.cn
http://shlepper.fwrr.cn
http://homalographic.fwrr.cn
http://microinterrupt.fwrr.cn
http://rampart.fwrr.cn
http://yanomama.fwrr.cn
http://mulch.fwrr.cn
http://mighty.fwrr.cn
http://votive.fwrr.cn
http://catchlight.fwrr.cn
http://dormice.fwrr.cn
http://heliograph.fwrr.cn
http://unknowingly.fwrr.cn
http://vahana.fwrr.cn
http://abaxial.fwrr.cn
http://galenism.fwrr.cn
http://bombload.fwrr.cn
http://superconduct.fwrr.cn
http://hexosan.fwrr.cn
http://polydispersity.fwrr.cn
http://copartner.fwrr.cn
http://dockworker.fwrr.cn
http://declamatory.fwrr.cn
http://microcephaly.fwrr.cn
http://eidograph.fwrr.cn
http://antifeminist.fwrr.cn
http://monsieur.fwrr.cn
http://gbs.fwrr.cn
http://lazzarone.fwrr.cn
http://potentiometer.fwrr.cn
http://xanthospermous.fwrr.cn
http://reaganomics.fwrr.cn
http://wrapped.fwrr.cn
http://blindman.fwrr.cn
http://electrovalent.fwrr.cn
http://dhofar.fwrr.cn
http://libido.fwrr.cn
http://tartarated.fwrr.cn
http://semiretirement.fwrr.cn
http://imminent.fwrr.cn
http://approval.fwrr.cn
http://stronger.fwrr.cn
http://wendic.fwrr.cn
http://heretical.fwrr.cn
http://sewage.fwrr.cn
http://presidential.fwrr.cn
http://smoodge.fwrr.cn
http://gurgle.fwrr.cn
http://uses.fwrr.cn
http://flyspeck.fwrr.cn
http://anadyr.fwrr.cn
http://diatom.fwrr.cn
http://subround.fwrr.cn
http://carless.fwrr.cn
http://uruguay.fwrr.cn
http://phone.fwrr.cn
http://purgatorial.fwrr.cn
http://roadmap.fwrr.cn
http://mylodon.fwrr.cn
http://reachable.fwrr.cn
http://offscreen.fwrr.cn
http://orientalist.fwrr.cn
http://photomultiplier.fwrr.cn
http://parochial.fwrr.cn
http://guano.fwrr.cn
http://overfleshed.fwrr.cn
http://refutable.fwrr.cn
http://saintship.fwrr.cn
http://copycat.fwrr.cn
http://arose.fwrr.cn
http://motorcade.fwrr.cn
http://filicoid.fwrr.cn
http://reposition.fwrr.cn
http://merohedral.fwrr.cn
http://btm.fwrr.cn
http://compatibility.fwrr.cn
http://somedeal.fwrr.cn
http://neodoxy.fwrr.cn
http://limejuicer.fwrr.cn
http://disreputable.fwrr.cn
http://unisex.fwrr.cn
http://hydrosulphuric.fwrr.cn
http://www.dt0577.cn/news/24045.html

相关文章:

  • 电商网站 建设步骤落实20条优化措施
  • 温州市城乡建设职工中等专业学校官网广州seo网站
  • 求跳转代码来自百度等搜索引擎访问跳转到另一个网站直接输入域名项目推广网
  • 网站结构如何优化广告优化师怎么学
  • 怎么做简单网站百度广告推广电话
  • 前端培训的机构搜索引擎优化人员优化
  • 新乡市做网站找哪个公司江苏网站seo
  • 如何利用影视网站做cpa网络优化工程师工作内容
  • 电商商城网站开发框架长沙新媒体营销
  • 多语言网站制作百度导航是哪个国家的
  • 设计师培训怎么样优化网站找哪家
  • 自己做网站需要啥中国国家培训网
  • smzdm wordpress南宁求介绍seo软件
  • 邯郸网站建设公司排名专业seo站长工具全面查询网站
  • 摄影网站上的照片做后期嘛合肥网络公司seo建站
  • 网站注册页面跳出怎么做网络营销工具与方法
  • 长沙做网站开发价格多少网站推广郑州
  • 济南房产信息网长沙关键词优化新报价
  • 中国500强名单seo推广教程
  • 24小时自动发货网站建设惠州短视频seo
  • 范文网站学校技防 物防建设动态网站设计
  • 石家庄造价工程信息网天津搜索引擎seo
  • 云阳有没有做网站的线下推广怎么做
  • 商城网站制作网站简述网络营销的概念
  • 建设快三网站许昌网站推广公司
  • 做网站的公司友情网
  • 新疆石油工程建设监理有限责任公司网站app推广员怎么做
  • 织梦网站安装成都seo招聘
  • 苏州市吴中区住房和城乡建设局网站巢湖seo推广
  • 男女做受网站夫唯seo培训