Vue基础整理之路由二次封装

news/2024/7/6 13:41:01 标签: vue.js, 前端, javascript

Vue路由的坑我可是踩了不少,那么我们就来看看小萌新的路由二次封装吧
封装路由首先我们要引入vue的路由

javascript">npm install vue-router --save

之后我们创建router.js 编写代码在router.js中编写

javascript">import Vue from 'vue'
import routes from './routes'
import Router from 'vue-router'
Vue.use(Router)
const router =  new Router({
  mode: 'history',
  routes: routes
})

//路由前置守卫相关操作
router.beforeEach((to, from, next) => {
  next()  
})
export default router

然后再同级建立routes.js
并在routes.js中编写具体路由定位

javascript">export default [
    {
      path: '/',
      redirect: '/HelloWorld',
    },
    {
      path: '/name',
      name: 'name',
      component: () => import('./components/logt.vue')
    },
    {
      path: '/HelloWorld',
      name: 'HelloWorld',
      component: () => import('./components/HelloWorld.vue')
    }
]

最后在main.js中引入router.js并挂在Vue上

javascript">import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false

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

首先我们需要在App.vue的某个位置使用标签来展示路由
路由是呈现在标签中的
在vue组件中调用用

javascript"> this.$router.push({
    path: "/name"
 })

即可完成跳转
还有就是大家会发现npm run build打包后index.html路由跳转不了了,其实很简单
把router.js里的mode: 'history’注释掉就好了


http://www.niftyadmin.cn/n/1038267.html

相关文章

字符串处理strlen函数需要注意的一些小细节问题

首先,strlen函数的原型是 extern unsigned int strlen(char *s);在Visual C 6.0中,原型为size_t strlen(const char *string); ,其中size_t实际上是unsigned int,在VC6.0中可以看到这样的代码:typedef unsi…

Vue基础整理之前置钩子 后置钩子

其实非常简单 在router.js中加入router.beforeEach和router.afterEach 函数就好了 代码如下 import Vue from vue import routes from ./routes import Router from vue-router Vue.use(Router) const router new Router({mode: history,routes: routes })//路由前置守卫相关…

ACdream1099 分治思想+快排优化+读入优化

完整题目描述: Problem Description 一天,萌萌的妹子--瑶瑶(tsyao)很无聊,就来找你玩。可是你们都不知道玩什么。。。尴尬了一阵子,机智的瑶瑶就提议:“这样吧,你说N个整数xi,然后在随意说一个数…

Vue将HTML内容导出为 word文档 excel工作表 pdf文档等文件

最少我现在还实现不了通用的导出方法 只能将这三种文件的导出方式分别写出来 无论你想转那种都是需要FileSaver的 所以我们先导入第三方工具包 npm install xlsx file-saver --saveexcel的导出方式,就在需要导出的组件位置这样编写就好了,参考代码如下 …

关于codeforces比赛规则介绍(转载)

Codeforces 简称: cf(所以谈论cf的时候经常被误会成TX的那款游戏). 网址: codeforces.com   这是一个俄国的算法竞赛网站,由来自萨拉托夫州立大学、由Mike Mirzayanov领导的一个团队创立和维护,是一个举办比赛、做题和交流的平台.举办比赛和做题就不说了,“交流”指的是自带b…

Vue将HTML内容用打印机打印出来

首先导入第三方插件 npm install vue-print-nb --save在main.js中引入 import Print from vue-print-nb Vue.use(Print);接着就算打印的组件了 <template> <div id"printTest">打印区 </div> <button v-print"#printTest">打印…

CF578B 贪心+预处理优化+思维到位

题目描述&#xff1a; D. "Or" Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can mu…

Vue引入Froala-Editor富文本编辑器

总是周知编辑器能让不懂变成语言的人也能写出一下html的基本标签&#xff0c;但坏处也非常明显&#xff0c;第一 编辑器所编辑出的内容是无法自适应的&#xff0c;第二 编辑器的内容无法调节响应式 但Froala却有一个不错的点&#xff0c;那就是他的图片可以设置百分比单位&…