惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
爱范儿
爱范儿
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
Y
Y Combinator Blog
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
M
MIT News - Artificial intelligence
博客园 - Franky
V
Visual Studio Blog
I
InfoQ
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
博客园 - 司徒正美
L
LangChain Blog

博客园 - dudu

test .NET CQRS 的实现中引入 ReadOnlyRepository 初试 .NET CQRS 开源库 LiteBus 什么是 Agentic ? 初试 Microsoft Agent Framework 初识 Microsoft Agent Framework:一句话介绍 ASP.NET Core 中读取 UserAgent 的正确姿势 记录一下对 ASP.NET Core Middleware 进行单元测试的代码 C# 实现通用的 IdEqualityComparer 用 Angular Signal Inputs 完成一个组件的重构 量子网络操作系统 QNodeOS 资料收集 Kubernetes 集群上部署 Open WebUI 在 Kubernetes 集群的 GPU 节点上部署 Ollama 尝试在 Kubernetes 集群上用阿里云 GPU 实例部署 Ollama + DeekSeek-R1 阿里云 GPU 实例云服务器本地部署 DeepSeek R1 尝试使用阿里云计算巢部署 DeepSeek-R1 Angular 中依赖注入问题造成 Observable 订阅不更新 Angular 中使用 ChildContent 记录 园子博客后台升级至 angular 19 后 eslint 9 迁移记录 学习大模型(LLM)的英文好文收集
园子博客后台 Angular 升级:手工迁移至 Standalone Component
dudu · 2025-01-09 · via 博客园 - dudu

最近在将园子博客后台的前端框架从 angular 15 升级至 angular 19,这边博文记录的是迁移至 Standalone Component 的过程。

之前尝试使用 angular 19 提供的迁移命令自动迁移,但迁移失败,详见 https://q.cnblogs.com/q/150498

ng g @angular/core:standalone

改为手动迁移,记录一下迁移过程。

删除标记与 NgModule 文件

  • 删除所有 standalone: false,一共 198
  • 删除所有 NgModule 文件,一个 84 个文件
find . -name *.module.ts -exec git rm {} \;
  • 恢复测试中使用的 NgModule 文件 testing.module.ts

迁移至 standalone bootstrapping api

  • 修改 main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';

bootstrapApplication(AppComponent, appConfig)
    .catch((err) => console.error(err));
  • 创建 app.config.ts 文件并将 app.module.ts 中的 providers 迁移过来
export const appConfig: ApplicationConfig = {
    providers: [
        provideHttpClient(withFetch()),
        provideAnimations(),
        provideZoneChangeDetection(
            {
                eventCoalescing: true
            }),
        provideRouter(
            routes,
            withRouterConfig({
                onSameUrlNavigation: 'reload'
            }),
            withPreloading(NoPreloading),
            withViewTransitions()
        ),
        NzModalService,
        LoggerService,
        // ...
    ]
};

针对 ngx-monaco-editor-v2,将

MonacoEditorModule.forRoot({
    baseUrl: environment.assets.monacoBase,
}),

改为

provideMonacoEditor({
    baseUrl: environment.assets.monacoBase,
}),
  • 恢复 app-routing.module.ts 文件并重命名为 app.routes.ts
git checkout HEAD~1 app-routing.module.ts
git mv app-routing.module.ts app.routes.ts
  • 处理 app.routes.ts 中通过 loadChildren 加载 NgModule 的代码
loadChildren: () => import('./pages/posts/posts.module').then(m => m.PostsModule)
  • 将 posts.module.ts 并转换为 posts.routes.ts
git checkout HEAD~1 ./pages/posts/posts.module.ts
git mv ./pages/posts/posts.module.ts ./pages/posts/posts.routes.ts
  • 修改 posts.routes.ts,改为 export const routes, 删除 NgModule 部分代码,修改路由部分代码
export const POSTS_ROUTES: Routes = [
    {
        path: '',
        component: PostMainComponent,
    }
]
  • 修改 app.routes.ts 中对应的 loadChildren 代码
loadChildren: () => import('./pages/posts/posts.routes').then(m => m.POSTS_ROUTES)
  • 在 app.config.ts 中添加 onSameUrlNavigation 与 preloadingStrategy 配置
export const appConfig: ApplicationConfig = {
    providers: [
        provideZoneChangeDetection(
            {
                eventCoalescing: true
            }),
        provideRouter(
            routes,
            withRouterConfig({
                onSameUrlNavigation: 'reload'
            }),
            withPreloading(NoPreloading)
        ),
        provideHttpClient(withFetch()),
    ]
};

添加 import

近 200 个 component 需要一个一个 component 添加所需的 import,这是迁移中工作量最大的部分,即使只是用到了 *ngIf 或者 [ngClass], 也要添加对应的 import,详见 https://q.cnblogs.com/q/151533

解决 ViewChild 的问题

详见博问:迁移到 Standalone Component 后 ViewChild 无法正常工作

处理 imports 中漏添加 Directive 的问题

一个 standalone component 的模板中用了某个 directive,如果没有在 imports 中添加,build 与运行是都不会报错,只是这个 directive 没有被执行。

在 app.routes.ts 添加 providers

将之前在各个 NgModule 中添加的 providers 迁移到 app.routes.ts 中

参考资料