Nuxt 路由---03

一、路由

Nuxt路由官网上的API详解:点击链接

Nuxt.js 依据 pages 目录结构自动生成 vue-router 模块的路由配置。

(1) 要在页面之间使用路由,我们建议使用 标签。 支持activeClass ,tag

基础路由:

layout/default.vue默认布局:

<template>
  <div>
    <!-- 跳转 -->
    <nuxt-link to="/user/one" tag="li" activeClass="active">filem</nuxt-link>
    <!-- <nuxt /> 一级路由容器 -->
    <nuxt />
  </div>
</template>

假设 pages 的目录结构如下:

pages/
--| user/
-----| index.vue
-----| one.vue
--| index.vue

那么,Nuxt.js 自动生成的路由配置如下:

router: {
  routes: [
    {
      name: 'index',
      path: '/',
      component: 'pages/index.vue'
    },
    {
      name: 'user',
      path: '/user',
      component: 'pages/user/index.vue'
    },
    {
      name: 'user-one',
      path: '/user/one',
      component: 'pages/user/one.vue'
    }
  ]
}

嵌套路由:

        1.创建内嵌子路由,你需要添加一个 Vue 文件,同时添加一个与该文件同名的目录 用来存放子视图组件。

        2.Warning: 别忘了在父组件(.vue文件) 内增加 用于显示子视图内 容。

看了官网上的api实现了官网的案例你会发现访问父页面中只能显示父页面中的内容,要想默认的在<nuxt-child>区域显示一个页面内容怎么办?

自己案例代码:

pages/parent.vue

<template>
    <div>
        <h2>父组件的页面的内容</h2>
        <ul>
            <!-- 进行切换子页面,写法同vue.js -->
            <li><nuxt-link to='/parent/child'>child</nuxt-link></li>
            <li><nuxt-link to='/parent/child2'>child2</nuxt-link></li>
        </ul>
        <hr>
        <div class="box">
            <p>嵌套子页面内容区</p>
            <!-- <nuxt-child>标签在父页面组件中相当于是子页面组件的占位符;嵌套中这个不可少 -->
            <nuxt-child keep-alive :foobar="123"></nuxt-child>
        </div>
    </div>
</template>
<script>
export default {
    
}
</script>
<style scoped>
.box{
    margin-top: 20px;
    padding: 20px;
    border: 2px solid pink;
    border-radius: 5px;
}
</style>

pages/parent/index.vue

<template>
    <div>
        <h2>嵌套子组件中的默认页面index.vue</h2>
    </div>
</template>
<script>
export default {
    
}
</script>
<style scoped>

</style>

pages/parent/child.vue

<template>
    <div>
        <h2>嵌套子组件中的页面child</h2>
        <p>foobar:{{foobar}}</p>
    </div>
</template>
<script>
export default {
    props:['foobar']
}
</script>
<style scoped>

</style>

pages/parent/child2.vue

<template>
    <div>
        <h2>嵌套子组件中的页面child2</h2>
        <p>foobar:{{foobar}}</p>
    </div>
</template>
<script>
export default {
    props: ['foobar']
}
</script>
<style scoped>

</style>

效果如下:

重定向:

        a. nuxt.config.js

    // 配置重定向
    router: {
        extendRoutes(routes, resolve) {
            routes.push({
                path: '/',
                redirect: '/film'
            })
        }
    },

         b. 利用中间件来处理

 // 中间件 middle/ redirect.js
 export default function({
     isHMR,
     app,
     store,
     route,
     params,
     error,
     redirect
 }) {
     if (isHMR) return
         // 页面均放在_lang文件夹下,即lang为动态路由参数
         /*if (!params.lang) { //此写法会出现路由重定向次数过多的问题
   return redirect('/' + defaultLocale + '' + route.fullPath)
   }
  */
     if (route.fullPath == '/film') {
         return redirect('/film/nowplaying')
     }
 }
 router: {
     middleware: 'redirect' // 即每次路由跳转会调用该中间件
         //多个中间件写法
         // middleware: ['redirect']
 }

动态路由:

在 Nuxt.js 里面定义带参数的动态路由,需要创建对应的以下划线作为前缀的 Vue 文件 或 目录。

pages/
‐‐| detail/
‐‐‐‐‐| _id.vue


//编程式跳转 this.$router.push("/detail");
//获取动态路由参数 <div>detail-{{ $route.params.id }}</div>

        获取动态路由参数

asyncData({params}){
    console.log(params.id);
}

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nuxt.js中,可以使用`<nuxt-link>`来实现带参数的跳转。具体的写法是在`<nuxt-link>`标签的`to`属性中使用`params`来传递参数。例如: ```html <nuxt-link :to="{ name: 'products', params: { keyword: item.name }}">{{ item.name }}</nuxt-link> ``` 这样就可以将`item.name`作为`keyword`参数传递给名为`products`的路由页面。 接受参数的方法有两种: 1. 在`asyncData(ctx)`方法中获取参数。可以使用`ctx.params.keyword`来获取通过参数传递的值。例如: ```javascript async asyncData(ctx) { console.log(ctx.params.keyword) // 可以获取到通过参数传递的值 } ``` 2. 在`created()`生命周期中获取参数。可以使用`this.$route.params.keyword`来获取通过参数传递的值。例如: ```javascript created() { console.log(this.$route.params.keyword) // 可以获取到通过参数传递的值 } ``` 需要注意的是,`<nuxt-link>`的使用方式与`<router-link>`相同,所以在接受参数方面也可以参考`<router-link>`的用法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [nuxt:nuxt-link的动态路由和传参](https://blog.csdn.net/weixin_45844049/article/details/105581675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值