Home > Web Front-end > Vue.js > A brief analysis of Vue sub-routing parameter transmission and reception

A brief analysis of Vue sub-routing parameter transmission and reception

藏色散人
Release: 2022-08-10 16:08:47
forward
1528 people have browsed it

This article will introduce you to vue routing: sub-routing, and the passing of parameters in routing. I hope it will be helpful to everyone!

1. Create a new vue project in idea

2. Modify in main.js

import Vue from 'vue'
// import Router from './Router'     /*引用自同级Router.js*/
import Router from './SonRouter'     /*引用自同级SonRouter.js*/
Copy after login

A brief analysis of Vue sub-routing parameter transmission and reception
[Related recommendations: vue.js Video tutorial

Create a new file SonRouter.js under 3.src

/*子路由*/
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
//声明模版,点击链接显示对应的内容
const first = { template:&#39;<div>first内容</div>&#39;}
const second = { template:&#39;<div>second内容</div>&#39;}
const Home = { template:&#39;<div>Home内容</div>&#39;}
const firstFirst = { template:&#39;<div>firstFirst内容 {{$route.params.id}} {{$route.params.name}}</div>&#39;}
const firstSecond = { template:&#39;<div>firstSecond内容 {{$route.params.id}} {{$route.params.name}}</div>&#39;}

//单独的写组件模版的时候可直接这样写,名称自定义
const sonRunterTemplate ={
  template:`
      <div class="">
            <h2>组件</h2>
            <router-view class="red"></router-view> 
       </div>
  `
}

//router自己定义名字
const router = new VueRouter({
  mode:&#39;history&#39;,
  base:__dirname, //当前的路径   dirname在node中指当前的本地路径
  routes:[       //以数组的方式给出  [{},{}],多个的话一定是routes复数形式
    {path:&#39;/&#39;,name:&#39;Home&#39;,component:Home},
    {path:&#39;/first&#39;,component:sonRunterTemplate,
        children:[
          {path:&#39;/&#39;,name:&#39;Home-First&#39;,component:first},
          {path:&#39;first&#39;,name:&#39;Home-First-First&#39;,component:firstFirst},
          {path:&#39;second&#39;,name:&#39;Home-First-Second&#39;,component:firstSecond}
        ]
    },
    {path:&#39;/second&#39;,name:&#39;Home-Second&#39;,component:second}
  ]
})

new Vue({
  router,
  template:`
    <div id=&#39;r&#39;>
        <h1>导航</h1>
        <p>{{$route.name}}</p>
         <ol>
            <li><router-link to="/">/</router-link></li>   <!--router-link存放路由链接的   to相当于href,表示链接到哪里-->
            <li><router-link to="/first">first</router-link></li>
                <ol>
                    <li><router-link :to="{name:&#39;Home-First-First&#39;,params:{id:147,name:&#39;张三&#39;}}">first</router-link></li>
                    <li><router-link :to="{name:&#39;Home-First-Second&#39;,params:{id:258,name:&#39;李四&#39;}}">second</router-link></li>
                </ol>
            <li><router-link to="/second">second</router-link></li>
          </ol>
          <router-view class="green"></router-view>   <!--规定整个路由显示在其中,class表示修饰的类-->
    </div>
    `
}).$mount(&#39;#app&#39;)
/*
路由中参数的传递:
    1.通过路由传参 name:&#39;Home-First&#39;   获取 <p>{{$route.name}}</p>
    2.绑定to方式进行参数的传递  :to="{name:&#39;Home-First-Second&#39;,params:{id:258,name:&#39;李四&#39;}}"  获取{{$route.params.id}} {{$route.params.name}}
    */
/*
route 路线  $route.params
router  路由
routes  路由复数形式  一定是数组
*/
Copy after login

Run results:
A brief analysis of Vue sub-routing parameter transmission and reception

The above is the detailed content of A brief analysis of Vue sub-routing parameter transmission and reception. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template