• Vue中的query传参和动态路由传参


    query传参

    2种传参:
    1、go
    2、this.$router.push({path:"/xx",query:{name:"karen",pwd:123}})

    //在路由匹配的组件中获取数据:
    mounted(){let queryObj=this.$route.query}

    动态路由传参


    设计:
    const router=new VueRouter({
         routes:[
             {path:"/home/:id",component:()=>import("./home.vue")},
             {path:"/about",component:()=>import("./about.vue")}]
     })
     
     2种传参:
     go
     this.$router.push({path:"/home",params:{id:123}})
    // 如果提供了 path,params 会被忽略,上述例子中的 query 并不属于这种情况。取而代之的是下面例子的做法,你需要提供路由的 name 或手写完整的带有参数的 path:
     
    //在路由匹配的组件中获取数据:
    mounted(){let paramsObj=this.$route.params}

    案例

    路由代码:

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import HomeView from '../views/HomeView.vue'
    4. Vue.use(VueRouter)
    5. const routes = [
    6. {
    7. path: '/',
    8. name: 'home',
    9. component: HomeView
    10. },
    11. {
    12. path: '/test1/:id', //动态路由
    13. name: 'test1',
    14. component: () => import('../views/test1View.vue')
    15. },
    16. {
    17. path:'/test2',
    18. name:'test2',
    19. component:()=>import('../views/test2View.vue')
    20. }
    21. ]
    22. const router = new VueRouter({
    23. routes
    24. })
    25. export default router

    主界面代码:

    1. <script>
    2. export default {
    3. name: 'HomeView',
    4. methods: {
    5. link1(arg) {
    6. this.$router.push(`/test1/${arg}`)
    7. },
    8. link2(arg1,arg2) {
    9. this.$router.push(`/test2?a=${arg1}&b=${arg2}`)
    10. },
    11. link3(arg1,arg2) {
    12. this.$router.push({path:'/test2',query:{a:arg1,b:arg2}})
    13. }
    14. }
    15. }
    16. script>
    17. <style lang="css" scoped>
    18. .test1 {
    19. background-color: antiquewhite;
    20. }
    21. .test2 {
    22. background-color: rgb(167, 230, 164);
    23. }
    24. style>

    test1界面代码

    1. <script>
    2. export default {
    3. name: 'VueTest1View',
    4. data() {
    5. return {
    6. msg:""
    7. };
    8. },
    9. mounted() {
    10. console.log(this.$route.params.id) //params去接收
    11. this.msg=this.$route.params
    12. },
    13. methods: {
    14. },
    15. };
    16. script>
    17. <style lang="sass" scoped>
    18. style>

    test2界面代码

    1. <script>
    2. export default {
    3. name: 'VueTst2View',
    4. data() {
    5. return {
    6. msg:""
    7. };
    8. },
    9. mounted() {
    10. console.log(this.$route)
    11. this.msg=this.$route.query //将传入的值保存起来
    12. },
    13. methods: {
    14. },
    15. };
    16. script>
    17. <style lang="sass" scoped>
    18. style>

    效果图:

     分别点击动态路由传参的各个链接得到的结果为

    1、

    2、

     

    3、

     

      分别点击query传参传参的各个链接得到的结果为

    1、

    2、

     

    3、

     

    4、

     

  • 相关阅读:
    glog与pugi::xml使用方法
    H264基本原理
    VL600威锋typeC 转HDMI转接单芯片方案,支持DP1.4两LANE实现4K60,
    Windows 11 Ubuntu子系统所在磁盘位置、访问windows磁盘、另一个程序正在使用文件进程无法访问解决方法
    Leetcode周赛368补题(3 / 3)
    【vue】下拉、上拉刷新
    Minifilter过滤驱动与R3程序通讯实现文件保护
    隧道代理 vs 普通代理:哪种更适合您的爬虫应用?
    mac 中配置idea自带maven环境变量
    Bootstrap-栅格实例(二)
  • 原文地址:https://blog.csdn.net/m0_63470734/article/details/126820080