当前位置: 首页 > news >正文

nextjs使用02

并行路由

同一个页面,放多个路由,,
在这里插入图片描述
目录前面加@,layout中可以当作插槽引入

import React from "react";

function layout({children,notifications,user}:{
    children:React.ReactNode,
    notifications:React.ReactNode,
    user:React.ReactNode
}){

    return (
        <div>layout
            {notifications}
            <div>{user}</div>
        </div>
    )
}

export default layout

nextjs拦截跳转页面

在这里插入图片描述

next写接口

创建route.ts,

export async function POST(request:Request){
    var comment = await request.json();

    const newComment = {
        id:comments.length+1,
        text:comment.text
    }

    comments.push(newComment)

    return new Response(JSON.stringify(newComment),{
        headers:{"Content-Type":"application/json"},
        status:201
    })
}
next接收queryParams
import {type NextRequest} from "next/server"
export async function GET(request:NextRequest,){
    var searchParams = request.nextUrl.searchParams;
    var query = searchParams.get("query");
    var find = query?comments.find(comment=>comment.text.includes(query)):comments;
    return Response.json(find)
}
获取请求头
  • 第一种
import {type NextRequest} from "next/server"
export async function GET(request:NextRequest,){

     var requestHeaders = new Headers(request.headers);
     console.log(requestHeaders.get("Authorization"))
    }
  • 第二种
import {type NextRequest} from "next/server"
import {headers} from "next/headers"
export async function GET(request:NextRequest,){
    const headersList =  await headers()
    console.log(headersList.get("Authorization"))

    return Response.json("xxx")
}
获取cookie

两种方式获取cookie在这里插入图片描述

在这里插入图片描述

middleware中间件拦截

middleware.ts跟app一个层级

import  type {NextRequest} from "next/server";
import {NextResponse} from "next/server"

export function middleware(request:NextRequest){
    console.log(request.nextUrl.pathname,"pathname")

    // 重定向: 地址会变 
    // return NextResponse.redirect(new URL("/",request.url))
    //  转发,, 浏览器url不变,内容变化
    return NextResponse.rewrite(new URL("/",request.url))
}


export const config = {
    // 将 /hello 转发到  / 
    matcher:"/hello"
}

中间件处理response:===> 设置cookie或者响应头

import  type {NextRequest} from "next/server";
import {NextResponse} from "next/server"

export function middleware(request:NextRequest){
    console.log(request.nextUrl.pathname,"pathname")
    var response = NextResponse.next();
    var themePreference = request.cookies.get("theme");
    if (!themePreference){
        response.cookies.set("theme","dark")
    }
    
    return response
}


export const config = {
    // 将 /hello 转发到  /
    matcher:"/hello"
}

相关文章:

  • MySQL在线DDL操作指南
  • 安全框架SpringSecurity入门
  • Window C++ Postmortem Debugger
  • opencv(C++)图像的读写、翻转、绘制、鼠标事件
  • 源码分析之Leaflet中control模块Control基类实现原理
  • Vue2 通过 Object.defineProperty 对哪些数组进行了特殊处理?
  • C语言中的内存管理:掌握动态分配的技巧
  • 雪花算法生成的主键存在哪些问题,为什么不能使用自增ID或者UUID做MySQL的主键
  • git 对比两种优化方法的性能
  • MySQL主从复制(二)
  • Go语言入门指南:从语法基础到核心特性解析
  • 【C++】mapset使用与实战 OJ题
  • ABAP RANGE表 OPTION 运算符 SIGN
  • 无人机数据链技术及运行方式详解!
  • python生成并绘制各种类型声音噪声
  • xfreerdp 的使用
  • Spring的 init-method, @PostConstruct, InitializingBean 对比
  • 【鸿蒙5.0】两个数组,点击事件两个数组数据进行了双向数据交换,双向绑定的原理是什么?
  • JVM——模型分析、回收机制
  • 学透Spring Boot — 007. 加载外部配置
  • 上海国际咖啡文化节开幕,北外滩集结了超350个展位
  • 美的集团一季度净利增长38%,库卡中国机器人接单增超35%
  • 阿迪达斯一季度营收增近13%,称美国加征关税对业绩带来不确定性
  • 举牌超200轮!中铁建7.76亿元竞得北京通州梨园宅地
  • 临港迎来鸿蒙智行“尚界”整车及电池配套项目,首款车型今秋上市
  • 电话费被私改成48元套餐长达数年,投诉后移动公司退补600元话费