【问题标题】:Azure Static Web App with Vue project routing not working带有 Vue 项目路由的 Azure 静态 Web 应用程序不起作用
【发布时间】:2021-07-13 23:39:41
【问题描述】:

我有一个使用 Azure 静态 Web 应用程序部署的 vue 项目。项目包含路由器(历史模式)功能。它在本地完美运行。但是在部署到 Azure 路径链接后无法正常工作。例如,当我尝试从浏览器导航访问 mysite.com/about 时,它会返回 404 错误。

public 文件夹中的staticwebapp.config.json 文件:(我以微软文档中的这个例子为例)

{
    "routes": [
      {
        "route": "/*",
        "serve": "/index.html",
        "statusCode": 200
      }
    ]
  }

我的路由器js文件:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/api',
    name: 'Api',
    component: () => import('../views/Api.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

【问题讨论】:

    标签: azure vue.js vue-router router azure-static-web-app


    【解决方案1】:

    正如其他人所提到的,adding "routes.json" to your public folder is deprecated。但是,如果您是新来这里并查看以前的答案,那么您对新做法的了解会变得更糟。

    您正在寻找 Azure 静态 Web 应用配置文件的答案。该文件应放在名为staticwebapp.config.json 的应用程序的根目录下。 Here's an example 可以包含的内容,来自文档。

    对于单页应用程序,您最感兴趣的是捕获服务器“不知道”的路由以及应排除哪些路径。

    这是一个 Vue 应用程序的示例文件:

    (如果您没有应该以特殊方式运行的路由,则不必指定它们)

    {
      "globalHeaders": {
        "content-security-policy": "default-src https: 'unsafe-eval' 'unsafe-inline'; object-src 'none'"
      },
      "navigationFallback": {
        "rewrite": "/index.html",
        "exclude": ["/img/*.{png,jpg,gif,webp}", "/css/*"]
      },
      "mimeTypes": {
        "custom": "text/html"
      }
    }
    

    【讨论】:

    • +1 我正在使用 Azure 静态 Web 应用程序 + Vue 3,可以确认此处提供的 navigationFallback 可以按预期工作。这个答案应该是公认的。
    • 因为我在任何地方都找不到解决方案:对我来说它不起作用,因为使用 VueJS 我使用的是 dist 文件夹,而 staticwebapp.config.json 需要位于 azure 输出文件夹中是地区。所以我不得不将文件放在 /public 中,然后构建将 /public 中的所有内容复制到 /dist。
    【解决方案2】:

    public 目录中创建一个名为routes.json 的文件,并添加以下内容:

    {
      "routes": [
        {
          "route": "/*",
          "serve": "/index.html",
          "statusCode": 200
        }
      ]
    }
    

    Documentation

    【讨论】:

    • routes.json 现已弃用
    【解决方案3】:

    刚刚添加了 navigationFallback 属性,现在它可以工作了!!!

     {
            "routes": [
              {
                "route": "/*",
                "serve": "/index.html",
                "statusCode": 200
              }
              
            ], 
        
             "navigationFallback": {
                  "rewrite": "/index.html",
                  "exclude": ["/images/*.{png,jpg,gif}", "/css/*"]
                }
               
          }
    

    【讨论】:

      猜你喜欢
      • 2022-08-23
      • 2022-12-08
      • 2017-08-30
      • 2021-05-12
      • 2021-12-16
      • 1970-01-01
      • 2020-12-10
      • 2011-07-18
      • 2018-08-15
      相关资源
      最近更新 更多