url - vuejs 历史模式路由

标签 url vue.js url-rewriting vuejs2 vue-router

如何使用历史记录模式路由到更长的 URL 路径?

示例:/catalog/[product-name]/p/[product-id]

打开历史记录模式后,网站一片空白。 在我的 CSS 和 JS 上出现以下类型的错误

Refused to execute script from '{URL}/catalog/[product-name]/p/dist/build.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

路由器代码:

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/catalog/my-product-name/p/MASN123U', name: 'product', component: product }
  ]
})

边注: 它在没有历史模式的情况下工作正常,只是 URL 读取

/#/catalog/[product-name]/p/[product-id]

Web.config 与文档一致

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="SPA Routes" stopProcessing="true">
                    <!-- match everything by default -->
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <!-- unless its a file -->
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <!-- or a directory -->
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <!-- or is under the /api directory -->
                        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                        <!-- list other routes or route prefixes here if you need to handle them server side -->
                    </conditions>
                    <!-- rewrite it to /index.html -->
                    <action type="Rewrite" url="/index.html" />
                </rule>`enter code here`
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

最佳答案

来自错误:

Refused to execute script from '{URL}/catalog/[product-name]/p/dist/build.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

看看你的 HTML 文件中的某个地方,你可能有这样一个脚本标签:

<script src="dist/build.js"></script>

当它的路径应该是绝对路径时,比如:

<script src="/dist/build.js"></script>

注意 dist 之前添加的 /

关于url - vuejs 历史模式路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984384/

相关文章:

php - 使用 Php Get 方法将 URL 中的空格更新为连字符

url - 将所有以 "#_=_"结尾的 URL 重定向到根域

python - 将文件名转换为文件 ://URL

vue.js - Vuetify 选择保留旧值

regex - 带有 %20 的 URL 显示 404 错误代码,如何解决?

Java URL - 服务器响应 416 错误代码

javascript - 无法导入/导出 vuejs 组件

javascript - Vuejs 将现有匿名对象分配给 data 属性中的属性

apache - 没有mod_rewrite的Apache重写?

php - htaccess 重写规则在提交表单时导致错误