本文介绍了如何从JavaScript读取mix-manifest.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用laravel-mix缓存了图像.对于刀片中的图像,它可以工作.但是我不知道如何从javascript访问该版本化路径.

I cached my images using laravel-mix. For the image in blade, it works. However I couldn't figure out how to access that versioned paths from javascript.

是否可以从已编译的javascript中读取mix-manifest.json?这样我就可以创建一个mixin并获取版本化路径

Is it possible to read mix-manifest.json from compiled javascript? So that I can create a mixin and get the versioned paths

例如,在我的mix-manifest.json中,

For example, in my mix-manifest.json I have

"/images/hello.svg": "/images/hello.svg?id=0360de485c68385550da",

,我认为如果可以从编译的javascript中读取mix-manifest.json,则可以创建类似

and I thought if I can read the mix-manifest.json from my compiled javascript, I can create a helper method like

export default {
  methods: {
     mixPath(path) {
        let manifestObj = // read mix-manifest.json
        return manifestObj[path];
     }
  }
}

推荐答案

我可以使用

const vm = this;

if (!Vue.prototype.BROWSER_BUILD) {
     let path = require("path");
     this.manifestPaths = JSON.parse(require('fs').readFileSync(path.resolve('./mix-manifest.json'), 'utf8'));
} else {
     axios.get('/mix-manifest.json').then(json => {
        vm.manifestPaths = json.data
     })
}

方法:

methods: {
    mix(path) {
        if (typeof this.manifestPaths[path] == 'undefined') {
            return this.manifestPaths[path];
        }

        return path;
    }
}


如果您知道更好的方法,我很高兴知道:)

这篇关于如何从JavaScript读取mix-manifest.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:10