Blazor_WASM之4:路由

Blazor_WASM之4:路由

路由模板

通过 Router组件可在 Blazor 应用中路由到 Razor 组件。 Router 组件在 Blazor 应用的 App 组件中使用。App组件模板如下

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        <p>Sorry, there's nothing at this address.</p>
    </NotFound>
</Router>

在运行时,RouteView组件有两个作用:

  • 从 Router 接收 RouteData(例如/test)以及所有路由参数。
  • 使用指定的组件的布局来呈现该组件,包括任何后续嵌套布局。

组件支持使用多个 @page 指令,/blazor-route和/different-blazor-route都能跳转到该界面

@page "/blazor-route"
@page "/different-blazor-route"

<h1>Blazor routing</h1>

也可以将@page替换成@attribute

@page "/test"
@attribute [Route("test")]

重要:想要正确解析URL,必须在<head>中包含<base>,也就是在wwwroot中的index.html中<head>中包含<base>

<head>
    ...
    <base href="/" />
    ...
</head>

元素聚焦

切换到指定页面后,可以将UI焦点设置到指定元素。

<FocusOnNavigate RouteData="@routeData" Selector="h1" />

当 Router 组件导航到新页面时,FocusOnNavigate组件将焦点设置到页面的顶层标题 (<h1>)。

NotFound

如果找不到所请求路由的内容,则 Router组件允许应用指定自定义内容

<NotFound>
     <h1>Sorry</h1>
     <p>Sorry, there's nothing at this address.</p>
</NotFound>

路由参数

路由器使用相同的路由参数名称来填充组件参数,且路由参数不区分大小写。

@page "/test/{name}"

<h3>Test</h3>

<p>传入的参数是:@Name</p>

@code {
    [Parameter]
    public string? Name{ set;get; }
}

在地址栏中填写/test/哈哈哈

image-20230308102203841

但是,如果地址栏只写/test,也就是不写参数,则会出现

image-20230308102326706

改写为@page "/test/{name?}",name则变成可选参数,地址栏仍然只写/test,则会出现

image-20230308102507905

路由约束

路由约束强制在路由段和组件之间进行类型匹配,例如只允许id为int类型

@page "/user/{Id:int}"

<h1>User Id: @Id</h1>

@code {
    [Parameter]
    public int Id { get; set; }
}

路由约束类型

image-20230308103322593

路由约束也可以使用可选参数,比如下面的Id是必须,option是可选

@page "/user/{Id:int}/{Option:bool?}"

CatchAll路由参数

如果想把/test/a/b/c/test/后面的内容全部当做参数,则可以使用/test/{*par}来进行提取。

@page "/catch-all/{*pageRoute}"

@code {
    [Parameter]
    public string? PageRoute { get; set; }
}

查询字符串

查询字符串的样式如/test?name=tom&age=18后面的内容就是查询字符串,查询字符串支持的类型有bool, DateTime, decimal, double, float, Guid, int, long, string。使用方法如下:

@page "/test"

<h3>Test</h3>

<p>姓名:@Name</p>
<p>年龄:@Age</p>
<p>性别:@(MyProperty)</p>

@code {

    [Parameter]
    [SupplyParameterFromQuery]
    public string? Name{ set;get; }

    [Parameter]
    [SupplyParameterFromQuery]
    public int? Age { set; get; }

    [Parameter]
    [SupplyParameterFromQuery]
    public bool Gender { set; get; }

    public string MyProperty
    {
        get { return Gender?"男":"女"; }
    }   
}

在地址栏输入.../test?name=Tom&age=18&gender=true

image-20230308114308555

SupplyParameterFromQuery的Name属性可以指定查询参数的名字,下面是一个将查询参数放到同一个数组的案例:

@page "/test"

<h3>Test</h3>
@foreach (var item in stars)
{
    <p>@item</p>
}

@code {

    [Parameter]
    [SupplyParameterFromQuery(Name ="star")]
    public string[]? stars { set; get; }

}

在地址栏输入.../test?star=a&star=b&star=c

image-20230308115557316

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

步、步、为营

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值