vue基础:路由跳转的几种方式。

灰太狼 2022-03-15 12:18 469阅读 0赞

$nbsp 小谷刚开始学习vue,可以说是完全不懂了。想要实现h5中的跳转,都成了是头疼的事,进过查资料,得出以下总结,希望能对大家有用,也是对知识的记载。
1.最简单的路由跳转方式。

  1. <router-link to="/home">我的订单</router-link>

这种方法也是小谷最早实现的。
1.首先给大家看下我的目录(创建项目的时候已经默认安装的了router,如果没有安装的大家百度回来再看)
在这里插入图片描述
2.在main.js配置。

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. //导入并使用
  6. import VueRouter from 'vue-router'
  7. import ElementUI from 'element-ui'//这个无所谓
  8. import 'element-ui/lib/theme-chalk/index.css'
  9. Vue.use(ElementUI)
  10. Vue.use(VueRouter)
  11. Vue.config.productionTip = false
  12. //1.创建组件
  13. import Home from './components/Home.vue';
  14. import News from './components/News.vue';
  15. import Ewallet from './components/Ewallet.vue';
  16. import Userset from './components/Userset.vue'
  17. //2.配置路由 注意,名字一定不能错
  18. const routes = [ //若这里是 xxx,那么第25行应是 routers:xxx
  19. { path: '/home', component: Home },
  20. { path: '/news', component: News },
  21. { path: '/ewallet', component: Ewallet },
  22. { path: '/userset', component: Userset},
  23. { path: '*', redirect: '/ewallet' }//默认跳转路由
  24. ]
  25. //3.实例化VueRouter 注意,名字一定不能错
  26. const router = new VueRouter({
  27. routes // (缩写)相当于 routes: routes
  28. })
  29. //4.挂载路由
  30. /* eslint-disable no-new */
  31. new Vue({
  32. el: '#app',
  33. router,
  34. components: { App },
  35. template: '<App/>'
  36. })
  37. //5.根组件的模板里放上这句话 <router-view></router-view>

3.Ewallet.vue中添加<router-link to="/home">我的home</router-link>

  1. <template>
  2. <div id="app-content">
  3. <router-link to="/home">我的home</router-link>
  4. </div>
  5. </template>
  6. <script> </script>
  7. <!-- Add "scoped" attribute to limit CSS to this component only -->
  8. <style scoped> </style>

3.这里用Home.vue演示,就将路由跳转,如果你想在Home.vue再跳出来,就在这里面添加

  1. <template>
  2. <div class="home">
  3. <h1>{
  4. { msg }}</h1>
  5. <h2>Essential Links</h2>
  6. </div>
  7. </template>
  8. <script> export default { name: 'HelloWorld', data () { return { msg: 'Welcome to Your Vue.js App' } } } </script>
  9. <!-- Add "scoped" attribute to limit CSS to this component only -->
  10. <style> </style>

点击前
在这里插入图片描述
点击后
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,469人围观)

还没有评论,来说两句吧...

相关阅读