Angular2 RC5没有ViewResolver

时间:2016-08-24 07:48:21

标签: angular

我正在将angular2项目更新为rc5。我有一个Html解析器。它工作得很好,我更新到rc5。我的问题是在rc5中删除了“ViewResolver”。解析器有什么选择?

import {Injectable, ComponentMetadata, ViewMetadata, Type} from '@angular/core';
import {Globalservice} from './Service/Globalservice';

export class AppResolver extends ViewResolver {
    resolve(component: Type): ViewMetadata {
        var view = super.resolve(component);

        if (view.templateUrl.trim().substring(view.templateUrl.lastIndexOf('.')+1).toUpperCase() == "HTML" && Globalservice.IsMobileDevice()) {

            var index = view.templateUrl.lastIndexOf('/') + 1;
            var newUrl: string = view.templateUrl.substr(0, index) + "m" + view.templateUrl.substr(index);

            view.templateUrl = newUrl;
        }
        return view;
    }

    doesFileExist(urlToFile): boolean {
        return false;
    }

}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果你只是想修改" templateUrl"来自元数据。

你可以创建一个动态代理来拦截"解决" " UrlResolver"的方法:

import { UrlResolver } from '@angular/compiler';

export class MyUrlResolverInterceptor {

  static attach() {
    var proxied = UrlResolver.prototype.resolve;
    UrlResolver.prototype.resolve = (baseUrl: string, url: string): string => {
      var _url = doSomethingToMakeTheUrlYouWant(url);
      return proxied.bind(proxied)(baseUrl, _url);
    };
  };
}

并在bootstrap root模块之前执行MyUrlResolverInterceptor.attach();

see demo here