SAP 电商云 Spartacus UI UrlMatcherService 的用法介绍 - External Route 工作的单步调试

这个 Service 类 outline 如下图所示:

运行时打印:

这是 Spartacus 团队实现的 Routing Module:

在 init 阶段进行路由配置。

configure 放法的作用:使用 Spartacus routing config 增强原生的 Angular Routes 配置。只能被调用一次。

router.config 包含了 Spartacus 认的路由配置和合作伙伴通过 provideConfig 传递的自定义配置:

遍历每个 route 记录,调用 configureRoutes 方法

configureRoute 方法,负责设置 Route 的 path 和 matcher 属性

Product detail 有一个专门的 matcher:

在 Angular 这种单页应用程序中,开发人员可以通过显示或隐藏与特定组件相对应的显示部分来更改用户所看到的内容,而不是去服务器获取页面。 当用户执行应用程序任务时,他们需要在开发人员定义的不同视图之间移动。

要处理从一个视图到下一个视图的导航,可以使用 Angular 路由器。 路由器通过将浏览器 URL 解释为更改视图的指令来启用导航。

这个 function Location 指向的函数实现,负责决定 product/:productCode/:name 路由是否应当激活:

点击 cart 图标之后,准备判断加载 Spartacus 的 cart 页面,还是加载 Commerce Cloud Accelerator 的页面

传进来的 route 参数:cart

黄色部分为 url 判断函数体:

icludePatternsexcludePatterns 就是我们在 AppModule 里定义的配置:

matched 为 true:

准备重定向cart 页面去:

location 赋值为:electronics-spa/en/USD/cart

浏览器里新的 url:http://localhost:4000/electronics-spa/en/USD/

如果我们使用 CLI 加上 --routing flag 创建 Angular 应用,那么生成的 AppModule 里,会自动导入 AppRoutingModule

import { browserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module'; // CLI imports AppRoutingModule
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    browserModule,
    AppRoutingModule // CLI adds AppRoutingModule to the AppModule's imports array
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

window.location.href is not a method, it’s a property that will tell you the current URL location of the browser. Changing the value of the property will redirect the page.

更改 window.location.href 属性,会导致页面重定向

而 window.open 会打开一个新窗口:

window.location.href = ‘http://www.google.com’; //Will take you to Google.

window.open() example: window.open(‘http://www.google.com’); //This will open Google in a new window.

angular 里的使用案例

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...
win11本地账户怎么改名?win11很多操作都变了样,用户如果想要...