问题描述
我有一个Angular应用程序,我想在其中使用自定义UrlResolver提供程序以添加一些缓存中断逻辑,如本问题所示().
I have an Angular application in which I'd like to use a custom UrlResolver provider in order to add some cache breaking logic, as seen in this question (https://stackoverflow.com/a/43289767/868914).
但是,似乎并没有像我通常所做的那样和上面的链接所建议的那样,可以使用提供程序覆盖默认的编译器UrlResolver.
However it doesn't seem that I can override the default compiler UrlResolver using a provider, as I would normally do and as the above link suggests.
这是一个显示我的意思的朋克: https://plnkr.co/edit/zFsdyfNIoPcmbLO7WafW?p =预览
Here is a plunker showing what I mean: https://plnkr.co/edit/zFsdyfNIoPcmbLO7WafW?p=preview
如果使用chrome(或其他出色的)开发工具调试器查看compiler.umd.js的源代码,并搜索UrlResolver并在"resolve"方法中添加一个空格,则可以看到默认实现是代替了我提供的课程.
If you use the chrome (or other good) dev tools debugger to view the source for compiler.umd.js and search for the UrlResolver and put a break in the 'resolve' method, you can see the default implementation is being used instead of my provided class.
我找不到原因,希望这里的某人知道原因/解决方案?
I cannot find a reason for this, hopefully someone on here knows a reason / solution?
这里有app.module代码(也可以在plunkr上看到)
Heres app.module code (as seen on the plunkr also)
//our root app component
import {Component, NgModule, VERSION} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import { UrlResolver } from "@angular/compiler";
@Component({
selector: 'my-app',
templateUrl: 'my-app.html',
})
export class App {
name:string;
constructor() {
this.name = `Angular! v${VERSION.full}`
}
}
class NewUrlResolver extends UrlResolver {
resolve(baseUrl: string, url: string): string {
console.log("Custom resolve");
return "rubbish";
}
}
@NgModule({
imports: [ BrowserModule ],
providers: [{
provide: UrlResolver, useClass: NewUrlResolver
}]
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
推荐答案
您应该在 CompilerOptions
中的 providers
中提供自定义的 UrlResolver
引导应用程序时:
You should provide your custom UrlResolver
as part of providers
in CompilerOptions
when bootstraping application:
platformBrowserDynamic().bootstrapModule(AppModule, {
providers: [{ provide: UrlResolver, useClass: NewUrlResolver
}]})
Plunker Example
这篇关于Angular UrlResolver不会被自定义提供程序覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!