Angular的JS 缓存问题

问题描述:.net mvc ng 项目,每个component,html页面第一次加载后都会产生本地缓存,存储下来,导致新的页面改动不能及时刷新出来。 
解决方案,.net项目中自带一个版本文件,需要自己发布项目时自己手动更改它,这里我把它在构建的时候自动增加这个version. 我们会写好一个.exe的file在build event中添加这个事件即可。然后就可以在项目中用这个版本号了。在js和html页面后面加这个参数?v=versionNumber即可解决。 

“(SolutionDir)Tools\AllOutput\DllVersionBuilder.exe""(SolutionDir)Tools\AllOutput\DllVersionBuilder.exe""(ProjectDir)Properties” 传入文件路径作为参数。 


.exe 的源代码如下:根据代码开发时间去生成唯一的版本 

private static void Main(string[] args) 
{ 
    if (args.Length == 1) 
    { 
    string path = args[0]; 
    string path2 = Path.Combine(path, “CommonAssemblyInfo.cs”); 
    if (File.Exists(path2)) 
    { 
        string text = File.ReadAllText(path2); 
        Regex regex = new Regex(“\d+.\d+.\d+.\d+”, RegexOptions.IgnoreCase); 
        MatchCollection matchCollection = regex.Matches(text); 
        if (matchCollection.Count > 0) 
            { 
                string value = matchCollection[0].Value; 
                DateTime now = DateTime.Now; 
                string newValue = string.Format(“{0}.{1}.{2}.{3}”, new object[] 
                { 
                    now.Year - 2014, 
                    now.Month, 
                    now.Day, 
                    now.Ticks.ToString().PadLeft(4, ‘0’).Substring(now.Ticks.ToString().Length - 4, 4) 
                }); 
                text = text.Replace(value, newValue); 
                File.WriteAllText(path2, text); 
            } 
        } 
    } 
}

用的时候先在mvc页面中导入变量。 
c#code拿到变量值。

public static class SystemInformationFactory 
{ 
    public static string AssemblyFileVersion { get; private set; }
}
    static SystemInformationFactory()
    {
        AssemblyFileVersion = GetAssemblyFileVersion();
    }

    private static string GetAssemblyFileVersion()
    {
        var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);

        if (attributes.Length == 0)
        {
            return string.Empty;
        }

        var fileInformation = attributes[0] as AssemblyFileVersionAttribute;

        if (fileInformation == null)
        {
            return string.Empty;
        }

        return fileInformation.Version;
    }
}

页面引用变量 
@{ string version = “?v=” + Acc.MvcApp.Helpers.SystemInformationFactory.AssemblyFileVersion; 
string environment = SysParamFactory.GetSysParam(“Environment”)}

 
angular js systemjs.config.js配置中添加js version。

第一种方法可以在引进packages 时添加。

// packages tells the System loader how to load when no filename and/or no extension
    packages: {
        app: {
            main: './Main',
            defaultExtension: 'js' + DefaultValues.Version
        },
        rxjs: {
            defaultExtension: 'js' + DefaultValues.Version
        },
        primeng: {
            defaultExtension: 'js' + DefaultValues.Version
        }

    }

也可以在’system.locate’ 中返回这个js。

(function (global) {
SystemJS.defaultJSExtensions = true;
//var syslocate = System.locate;
//System.locate = function (local) {
//    var system = this;
//    return Promise.resolve(syslocate.call(this, local).then(function (address) {
//        return address + DefaultValues.Version;
//    }));
//};

html 页面添加版本。 这里用一个静态方法,在每个component 引入页面时候, 添加这个版本号。

declare var DefaultValues: any;
public static getURLVersion(url: string): string {
    return url + DefaultValues.Version;
}

运行项目可以在dubug环境中看到结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值