Remix?路由模塊輸出對象handle函數(shù)
正文
Remix handle 函數(shù)是一個有用的對外輸出的 Route 模塊對象,用于暴露特定的數(shù)據(jù) match 對象,它們經(jīng)常在一起使用。
當前 Remix 版本:1.15.0
在哪里可以定義 handle?
- root 根組件
- 路由頁面
在根路由定義
import { /.../ } from "@remix-run/react";
// 根路由 handle 配合頁面中 useMatches 獲取到 app 數(shù)據(jù)
export const handle = {
app: 1
}
export default function App() {
return (
<html lang="en">
// ...
</html>
);
在頁面 _index 路由中與 useMatch 一起
handle 與 useMatch 一起使用, useMatch 返回路由匹配相關(guān)的對象:
import type { V2_MetaFunction } from "@remix-run/node";
// hooks
import { useMatches } from "@remix-run/react";
export const meta: V2_MetaFunction = () => {
return [{ title: "New Remix App" }];
};
// 輸出定義 handle 對象
export const handle = {
test: 1,
}
export default function Index() {
const match = useMatches()
console.log(match[1].test) // 在 match 中訪問 match 函數(shù)
return (
<div>
<h1>Welcome to Remix</h1>
</div>
);
}
match 數(shù)組
match 是一個數(shù)組, 數(shù)組中的對象數(shù)據(jù)結(jié)構(gòu):
- data: 當前 loader 函數(shù)返回的數(shù)據(jù)
- handle: 當前路由定義的 handle 數(shù)據(jù)
- id:當前的路由 id
- params: 當前的參數(shù)
- pathname: 當前的路由路徑
match 一般是一個數(shù)組,會有兩個對象:
- root.tsx 中的 match 對象
- 當前路由的 match 對象
使用場景
當路由中需要指定一些特定的數(shù)據(jù)的時候
- Remix-118i 中需要指定 handle
export const handle = { i18n: "login" };
i18n 提供給 Remix-i18n 用于根據(jù)當前路由匹配。
引用
以上就是Remix 路由模塊輸出對象handle函數(shù)的詳細內(nèi)容,更多關(guān)于Remix 路由模塊handle的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
阿里低代碼框架lowcode-engine自定義設(shè)置器詳解
這篇文章主要為大家介紹了阿里低代碼框架lowcode-engine自定義設(shè)置器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
在React中寫一個Animation組件為組件進入和離開加上動畫/過度效果
這篇文章主要介紹了在React中寫一個Animation組件為組件進入和離開加上動畫/過度效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06

