Aurelia仅在视图存在时才返回viewmodule的备用视图

时间:2017-07-27 13:24:15

标签: aurelia aurelia-cli

aurelia框架是否提供了检查视图是否存在的方法?

ViewLocator.prototype.convertOriginToViewUrl = (origin) => {

    let device = 'mobile';

    let moduleId = origin.moduleId.replace('.js', '').replace('.ts', '');      

    let deviceView = `${moduleId}-${device}.html`;

    //check if view exist and return if so
    if (ViewExist(deviceView)) {
        return deviceView;
    }

    //return default view
    return `${moduleId}.html`;
}

function ViewExist(view): boolean {
    //check filessytem?
    //check app-bundle?
    //any available api in aurelia to check?
    return false;

}

我尝试过使用aurelia-fetch-client中的HttpClient,但我想因为将文件捆绑到app-bundle中这个选项是不可能的。

let http = new HttpClient();
let response = http.fetch('view-mobile.html');

1 个答案:

答案 0 :(得分:0)

您可以实现自己的视图定位器并替换一个框架使用

https://github.com/aurelia/templating/blob/7693dbd65e59e428e4052922920145b76240f3cf/src/view-locator.js

您可以通过在DI容器中注入viewLocator

来实现

另请查看有关自定义约定的文档 http://aurelia.io/docs/fundamentals/app-configuration-and-startup#customizing-conventions