zoukankan      html  css  js  c++  java
  • vue 中query和pramas中的那点事

    //$router : 是路由操作对象,只写对象

    //$route : 路由信息对象,只读对象

    //操作 路由跳转

    this.$router.push({
    name:'hello',
    params:{
       name:'word',
       age:'11'
    }
    })

    //读取 路由参数接收

    this.name = this.$route.params.name;
    this.age = this.$route.params.age;

    query传参要用path来引入(name引入也可以),params传参要用name来引入

    
    

    //query传参,使用name跳转


    this.$router.push({
    name:'second',
    query: {
        queryId:'20180822',
        queryName: 'query'
    }
    })

    
    

    //query传参,使用path跳转
    this.$router.push({
    path:'second',
    query: {
        queryId:'20180822',
        queryName: 'query'
    }
    })

    
    

    //query传参接收
    this.queryName = this.$route.query.queryName;
    this.queryId = this.$route.query.queryId;

    2·params传递参数

    注:使用params传参只能使用name进行引入

    //params传参 使用name
    this.$router.push({
      name:'second',
      params: {
        id:'20180822',
         name: 'query'
      }
    })
    
    //params接收参数
    this.id = this.$route.params.id ;
    this.name = this.$route.params.name ;
    
    //路由
    
    {
    path: '/second/:id/:name',
    name: 'second',
    component: () => import('@/view/second')
    }

    需要注意的是:

    params是路由的一部分,必须要在路由后面添加参数名。query是拼接在url后面的参数,没有也没关系。
    params一旦设置在路由,params就是路由的一部分,如果这个路由有params传参,但是在跳转的时候没有传这个参数,会导致跳转失败或者页面会没有内容。
    如果路由后面没有 /:id/:name    地址栏没有参数 但是如果你刷新一下,就会发现页面获取参数失败  

    因此我们不可能让用户不要刷新,所以我们必须在路由后面加上 /:id/:name

    //params传参 使用path
    this.$router.push({
    path:'second',
    params: {
    id:'20180822',
    name: 'query'
    }
    })

    //params接收参数
    this.id = this.$route.params.id ;
    this.name = this.$route.params.name ;


    总结

    传参可以使用params和query两种方式。

    使用params传参只能用name来引入路由,即push里面只能是name:’xxxx’,不能是path:’/xxx’,因为params只能用name来引入路由,如果这里写成了path,接收参数页面会是undefined!!!。

    使用query传参使用path来引入路由。

    params是路由的一部分,必须要在路由后面添加参数名。query是拼接在url后面的参数,没有也没关系。

    二者还有点区别,直白的来说query相当于get请求,页面跳转的时候,可以在地址栏看到请求参数,而params相当于post请求,参数不会再地址栏中显示。

    解决问题: vue  通过 name 和 params 进行调整页面传参刷新参数丢失问题

    export default new Router({
      routes: [
    
        {
          path: '/',
          redirect: '/main',
        },{
          path: '/main',
          name: 'Main',
          component: ()=> import('@/views/Main'),
          children: [
            {
              //path: '/testPage',  //这种方式 不配置参数名, 页面刷新会丢失参数
               path: '/testPage/:aaa/:bbb',  //这样通过 name 和 params 进行路由传参时 , 刷新页面就不会丢失 参数aaa 和 bbb 了。
              name: 'TestPage',
              component: ()=> import('@/views/TestPage/TestPage')
            },
          ]
    
        },
    
      ]
    })

    methods: {
    
                //路由调整传参测试
    
                goRouterTest(){
                    // this.$router.push('/testpage');
                    this.$router.push({ name: 'TestPage', params:{aaa: '111', bbb: '222'} });
                }
    
            },
     

    如果用params传值为对象情况下  刷新页面就消失

    解决办法   用 JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。然后再用组件内部JSON,parse()再次转为对象。 或者选中vuex方式传值  在组件内容用计算属性获取。

    路由组件传参

    在组件中使用 $route 会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性。

    使用 props 将组件和路由解耦:取代与 $route 的耦合

    const User = {
      template: '<div>User {{ $route.params.id }}</div>'
    }
    const router = new VueRouter({
      routes: [{ path: '/user/:id', component: User }]
    })
    const User = {
      props: ['id'],
      template: '<div>User {{ id }}</div>'
    }
    const router = new VueRouter({
      routes: [
        { path: '/user/:id', component: User, props: true },
    
        // 对于包含命名视图的路由,你必须分别为每个命名视图添加 `props` 选项:
        {
          path: '/user/:id',
          components: { default: User, sidebar: Sidebar },
          props: { default: true, sidebar: false }
        }
      ]
    })

    通过 props 解耦 如果 props 被设置为 trueroute.params 将会被设置为组件属性。

  • 相关阅读:
    网络协议栈(6)RFC793TCP连接时部分异常流程及实现
    网络协议栈(5)sendto/send返回成功意味着什么
    LeetCode——Detect Capital
    LeetCode——Find All Numbers Disappeared in an Array
    LeetCode——Single Number
    LeetCode——Max Consecutive Ones
    LeetCode——Nim Game
    LeetCode——Reverse String
    LeetCode——Next Greater Element I
    LeetCode——Fizz Buzz
  • 原文地址:https://www.cnblogs.com/ddqyc/p/15534468.html
Copyright © 2011-2022 走看看