V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
aglsv
V2EX  ›  Vue.js

vue 打包后的资源地址

  •  
  •   aglsv · 2022-07-13 16:11:15 +08:00 · 1551 次点击
    这是一个创建于 624 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在 vue.config 文件里面添加了 publicPath:'./', ,之后看打包出来的 index.html ,虽然资源引用的地址确实为相对的,static/js/app.e6cc3797.js,但是在发布上去之后引用的资源却是主域名+static ,访问到根目录上去了,我试了下手动把 index.html 改成./static/js/app.e6cc3797.js的话是能够正常访问到相对目录的。 请问如果想在打包时就变成./static/js/app.e6cc3797.js,vue.config 里面该怎么配置呢

    16 条回复    2022-07-13 17:20:44 +08:00
    ifdef
        1
    ifdef  
       2022-07-13 16:15:16 +08:00
    这样?

    base: process.env.NODE_ENV === 'production' ? './' : '/',
    ifdef
        2
    ifdef  
       2022-07-13 16:21:20 +08:00
    我搜了下 vue.config 的写法是

    https://cli.vuejs.org/config/#publicpath

    官方文档上就有...
    thinkershare
        3
    thinkershare  
       2022-07-13 16:23:55 +08:00
    @aglsv 你的服务器是怎么配置的, 正常情况下: static/main.js 和./static/main.js 应该是一样的, 而使用域名+statc, 应该是必须写成 /static/main.js
    aglsv
        4
    aglsv  
    OP
       2022-07-13 16:24:05 +08:00
    @ifdef 是想说 baseurl ?这个 3.3 版本后就都改成 publicPath 了
    aglsv
        5
    aglsv  
    OP
       2022-07-13 16:26:07 +08:00
    @thinkershare 感觉像是服务区那块抓不到相对的目录,就直接抓到根目录上面去了,服务器这块不是我配置的,不清楚咋整的
    ifdef
        6
    ifdef  
       2022-07-13 16:26:44 +08:00
    @aglsv #4 我那个是 vite.config ,就是 base ,你把我那个 base 换成你的 publicPath 应该就可以了
    aglsv
        7
    aglsv  
    OP
       2022-07-13 16:27:44 +08:00
    @ifdef 我就是写了没效果,问题第一句就是
    Alon9
        8
    Alon9  
       2022-07-13 16:28:48 +08:00
    就是改 publicPath
    ifdef
        9
    ifdef  
       2022-07-13 16:29:32 +08:00
    @aglsv #7
    By default, Vue CLI assumes your app will be deployed at the root of a domain, e.g. https://www.my-app.com/. If your app is deployed at a sub-path, you will need to specify that sub-path using this option. For example, if your app is deployed at https://www.foobar.com/my-app/, set publicPath to '/my-app/'.

    The value can also be set to an empty string ('') or a relative path (./) so that all assets are linked using relative paths. This allows the built bundle to be deployed under any public path, or used in a file system based environment like a Cordova hybrid app.
    aglsv
        10
    aglsv  
    OP
       2022-07-13 16:34:40 +08:00
    @ifdef emm ,直接给 publicPath 写成子路径的名字是可以,不过我想问的是能否实现让打包后的文件里面的资源引用地址为./static ,服务器那边不是我配的没法改,现在就是虽然./static 和直接 static 都是相对的,但是只有./static 正常访问到子路径,所以才有了这么个问题
    thinkershare
        11
    thinkershare  
       2022-07-13 16:34:51 +08:00
    @aglsv 没有抓不到一说, 你在浏览器的调试控制台看下, 它实际的请求路径是什么, 服务器不知道有相对路径这回事. 你看下浏览器实际发起请求的完整 URL 地址. 服务器发起的地址都是绝对地址, 找不到应该返回 404 或者其它重定向.
    thinkershare
        12
    thinkershare  
       2022-07-13 16:37:02 +08:00
    @aglsv 正常情况, 这 2 种写法, 浏览器应该发起的都是同一个路径, 也就是:protocol://host:port/$index_html_path/static/js/app.e6cc3797.js, 服务器收到的请求地址应该是同一个才对
    aglsv
        13
    aglsv  
    OP
       2022-07-13 16:38:36 +08:00
    @thinkershare 控制台里面的资源的路径请求就全是请求到根目录上去了,但是本身打开的封好的 index.html 文件里的资源链接都是 static/js/app.e6cc3797.js 这种相对的,打开的网址也是主域名+子路由+index.html 。搞不懂这个了
    thinkershare
        14
    thinkershare  
       2022-07-13 16:47:51 +08:00
    @aglsv 你审查过 HTML 的实时元素吗? 我刚刚写了测试了一下, 完全没问题啊. 这个原理上来说, 和后端都应该没啥关系.
    ![图片]( https://imgur.com/a/xtDdatf)
    dream4ever
        15
    dream4ever  
       2022-07-13 17:07:52 +08:00
    你用的 Nginx 还是啥?检查一下配置?
    YhXyt
        16
    YhXyt  
       2022-07-13 17:20:44 +08:00
    nginx 配置了 location /static/吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5941 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:36 · PVG 10:36 · LAX 19:36 · JFK 22:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.