Vue-router[嵌套][动态]

一.Vue-router

1.基础

1.1 设置导向

# 转换为html就是a标签,to指向前端路由
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>

1.2 设置展示内容区域

解释:该代码放置在哪里,调用路由注册的模板就在哪里显示

<router-view></router-view>

1.3 路由配置

解释:component对应模板,在路由里面注册,无需外面再注册


    const Foo={
        template: `11111`
    }
    const Bar={
        template: `11111`
    }
const router = new VueRouter({
  routes: [
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar }
]
})

1.4 配置到Vue中

new Vue({
	el:'app',
	router:router
})

示例代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <SCript src="vue2.js"></SCript>
    <script src="vue-router3.js"></script>
</head>

<body>
    <div id="app">
        <router-link to="/foo">Go to Foo</router-link>
        <router-link to="/bar">Go to Bar</router-link><br>
        show in this
        <router-view></router-view>
    </div>
</body>
<script>
    const Foo = {
        template: `<p>123</p>`
    }
    const Bar = {
        template: `<p>66654354345345</p>`
    }
    const router = new VueRouter({
        routes: [
            { path: '/foo', component: Foo },
            { path: '/bar', component: Bar }
        ]
    })
    var app = new Vue({
        'el': '#app',
        router: router
    })



</script>

</html>

2.路由重定向

{ path: '/', redirect: '/foo'}

3.路由嵌套

解释:在该路由下显示其它路由,只需要添加children属性,再在对应模板里面写入导向嵌套内容的router-link和router-view

 const Foo={
        template: `<div>
            <div>1555115</div>
            <hr>
            <router-link to="/foo/zl">A</router-link>
            <router-link to="/foo/zll">B</router-link>
            <router-view></router-view>
        </div>`
    }
const router = new VueRouter({
        routes: [
            { path: '/foo', component: Foo ,children:[
                { path: '/foo/zl', component:FOOA},
                { path:'/foo/zll', component:FOOB}
            	]
            }
        ]
    })

4.动态路由

格式: { path: '/foo/:char', component:FOOA}
获取值:

    const FOOA={
        template: `<div>12222---{{$route.params.char}}</div>`
    }

5.路由参数传递

5.1 props为ture

目的:只去传递动态值
{ path: '/foo/:char', component:FOOA,props:true}

props:['char']
template: `<div>12222---{{char}}</div>`

5.2 props为对象

目的:只去传递普通值
{ path: '/foo/:char', component:FOOA,props:{'name':'jack'}}

props:['name']
template: `<div>12222---{{name}}</div>`

5.3 props为函数

目的:即想传递动态值,又想传递普通值
{ path: '/foo/:char', component:FOOA,props:route=>({'name':'jack','id':route.params.char})}

props:['name','id'],
template: `<div>12222---{{name}}+{{id}}</div>`

6.路由传递数据

6.1子传父

A:

  1. 子:this.$emit('confirm', 1)

B:

  1. 父:<router-view @confirm='changefor_flag'></router-view>
  2. methods里面写入changefor_flag函数

6.2兄弟跳转传值

A:

  1. 兄弟A写跳转
    editor (id) {
      this.$router.push({
        path: '/article/release',
        query: {
          id: id
        }
      })
    }

B:
2. 兄弟B承接

  mounted () {
    console.log(this.$route.query)
  }
  # 关键在this.$route.query

7.命名路由

解释:为了方便表示路由,给路由规则起了个名字,能够更佳方便的去表示
<router-link :to="{name:'go',params:{char:1551}}">B</router-link>#记得加冒号

 { path: '/foo/:char', name:'go',component:FOOA}

8.编程式导航

8.1 跳转

解释:主要用了this.$router.push()

const Foo={
        template: `<div>

            <button @click='gogo'></button>

        </div>`,
        methods:{
            gogo(){
                this.$router.push('/foo/15155151515155')
            }
        }
    }

8.2 前进后退

解释:主要用了this.$router.go(-1) 该参数表示后退一页

    const Foo={
        template: `<div>

            <button @click='gogo'></button>

        </div>`,
        methods:{
            gogo(){
                this.$router.go(-1)
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Router 是一个官方提供的 Vue.js 的路由管理器,可以用于构建单页面应用程序。嵌套路由是指在一个路由的组件中使用另一个路由。 在 Vue Router 中,可以通过在路由配置文件中定义嵌套路由。嵌套路由的配置是以树形结构来组织的,父级路由将会嵌套渲染其子路由的组件。 下面是一个示例的路由配置文件,演示了如何使用嵌套路由: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) const routes = [ { path: '/', component: Home, children: [ { path: '', component: Dashboard }, { path: 'about', component: About }, { path: 'products', component: Products, children: [ { path: '', component: ProductList }, { path: ':id', component: ProductDetail } ] } ] } ] const router = new VueRouter({ routes }) export default router ``` 在上面的代码中,父级路由 '/' 下包含了三个子路由:Dashboard、About 和 Products。而 Products 路由又包含了两个子路由:ProductList 和 ProductDetail。 在组件中使用嵌套路由时,需要在父级组件中使用 `<router-view>` 标签来渲染子路由的内容。 ```html <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 在父级组件的模板中,通过使用 `<router-view>` 标签,子路由的内容将会被渲染在这个位置。 这就是 Vue Router嵌套路由的基本使用方法。通过嵌套路由,可以更好地组织和管理应用程序的路由结构,实现更复杂的页面布局和导航功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值