路由跳转的方式

红太狼 2024-04-19 12:10 152阅读 0赞

router-view 实现路由内容的地方,引入组件时写到需要引入的地方 需要注意的是,使用vue-router控制路由则必须router-view作为容器。

通过路由跳转的几种方式

1、router-link 【实现跳转最简单的方法】

<router-link to='需要跳转到的页面的路径>
浏览器在解析时,将它解析成一个类似于<a> 的标签。

  1. <li >
  2. <router-link to="keyframes">点击验证动画效果 </router-link>
  3. </li>

别忘记给需要跳转的路径在需要提前在router/index.js下引入哦。

别忘记给需要跳转的路径声明哦

  1. 1. 不带参数
  2. <router-link :to="{name:'home'}">
  3. <router-link :to="{path:'/home'}"> //name,path都行, 建议用name
  4. // 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。
  5. 2.带参数
  6. <router-link :to="{name:'home', params: {id:1}}">
  7. // params传参数 (类似post)
  8. // 路由配置 path: "/home/:id" 或者 path: "/home:id"
  9. // 不配置path ,第一次可请求,刷新页面id会消失
  10. // 配置path,刷新页面id会保留
  11. // html 取参 $route.params.id
  12. // script 取参 this.$route.params.id
  13. <router-link :to="{name:'home', query: {id:1}}">
  14. // query传参数 (类似get,url后面会显示参数)
  15. // 路由可不配置
  16. // html 取参 $route.query.id
  17. // script 取参 this.$route.query.id
  1. this.$router.push() (函数里面调用)

20190918104020739.png

  1. 1. 不带参数
  2. this.$router.push('/home')
  3. this.$router.push({name:'home'})
  4. this.$router.push({path:'/home'})
  5. 2. query传参
  6. this.$router.push({name:'home',query: {id:'1'}})
  7. this.$router.push({path:'/home',query: {id:'1'}})
  8. // html 取参 $route.query.id
  9. // script 取参 this.$route.query.id
  10. 3. params传参
  11. this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
  12. // 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
  13. // 不配置path ,第一次可请求,刷新页面id会消失
  14. // 配置path,刷新页面id会保留
  15. // html 取参 $route.params.id
  16. // script 取参 this.$route.params.id
  17. 4. queryparams区别
  18. query类似 get, 跳转之后页面 url后面会拼接参数,类似?id=1, 非重要性的可以这样传, 密码之类还是用params刷新页面id还在
  19. params类似 post, 跳转之后页面 url后面不会拼接参数 , 但是刷新页面id 会消失

在helloworld.vue文件中

  1. <template>
  2. .....
  3. <li @click="change">验证路由传参</li>
  4. </template>
  5. <script>
  6. export default {
  7. data () {
  8. return {
  9. id:43, //需要传递的参数
  10. }
  11. },
  12. methods:{
  13. change(){
  14. this.$router.push({ //核心语句
  15. path:'/select', //跳转的路径
  16. query:{ //路由传参时push和query搭配使用 ,作用时传递参数
  17. id:this.id ,
  18. }
  19. })
  20. }
  21. }
  22. }
  23. </script>

在select.vue文件中

  1. <template>
  2. <select>
  3. <option value="1" selected="selected">成都</option>
  4. <option value="2">北京</option>
  5. </select>
  6. </template>
  7. <script>
  8. export default{
  9. data(){
  10. return{
  11. id:'',
  12. }
  13. },
  14. created(){ //生命周期里接收参数
  15. this.id = this.$route.query.id, //接受参数关键代码
  16. console.log(this.id)
  17. }
  18. }
  19. </script>

3. this.$router.replace() (用法同上,push)

4. this.$router.go(n) ()

  1. this.$router.go(n)
  2. 向前或者向后跳转n个页面,n可为正整数或负整数
  3. ps : 区别
  4. this.$router.push
  5. 跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面
  6. this.$router.replace
  7. 跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面 (就是直接替换了当前页面)
  8. this.$router.go(n)
  9. 向前或者向后跳转n个页面,n可为正整数或负整数

发表评论

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

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

相关阅读

    相关 方式

    router-view 实现路由内容的地方,引入组件时写到需要引入的地方 需要注意的是,使用vue-router控制路由则必须router-view作为容器。 ...

    相关 【Vue】

    [【Vue】路由跳转][Vue] 路由跳转有几种方式,我用的最多的是$router.push的方法: 实践: 例如下面的页面,要求点详情跳转到详情页。 ![在这里

    相关 【Vue】

    路由跳转有几种方式,我用的最多的是$router.push的方法: 实践: 例如下面的页面,要求点详情跳转到详情页。 ![在这里插入图片描述][watermark_