欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > vue2 配置router

vue2 配置router

2025/2/24 15:17:16 来源:https://blog.csdn.net/weixin_44565776/article/details/142575232  浏览:    关键词:vue2 配置router

项目场景:

在从0到1搭建前端项目时,经常需要配置路由router。


配置步骤

1、下载依赖

npm install vue-router@3     

2、创建router/index.js。

import Vue from "vue";
import  VueRouter from "vue-router"Vue.use(VueRouter)const routes = [{path:"/",//首页默认加载路由redirect:'/pdf'},{path:'/pdf',//路由名称//使用路由懒加载component:()=>import('../views/pdf.vue')//页面路径,其中pdf.vue一定要创建}]const router = new VueRouter({mode:'hash',//路由有hash 和history,一般本地开发就用hash,如果需要配置history,需要配置服务器routes
})export default router

3、加载router。

在main.js中加载router

import Vue from 'vue'
import App from './App.vue'
import router from './router'
//这个是加载的将html转pdf
//import htmlToPdf from '@/utils/htmlToPdf'//Vue.use(htmlToPdf)
Vue.config.productionTip = falsenew Vue({router,render: h => h(App),
}).$mount('#app')

4、配置页面。在App.vue中配置路由

<template><div id="app"><router-view></router-view></div>
</template><script>
// import HelloWorld from './components/HelloWorld.vue'export default {name: 'App',components: {// HelloWorld}
}
</script><style>
#app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;margin-top: 60px;
}
</style>


收获总结:

现在的项目比较老,使用的vue2,所有又重新温习一遍vue。温故而知新。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词