具有 Vue 项目路由的 Azure 静态 Web 应用程序无法正常工作

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

我有一个使用 Azure Static Web App 部署的 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

最佳答案

正如其他人提到的,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"
  }
}

关于具有 Vue 项目路由的 Azure 静态 Web 应用程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67167681/

相关文章:

javascript - 当数据来自 ajax 时,Vue 不会更新 DOM

vue.js - 我怎么能在组合 api 中观看 Prop ?

javascript - Vue.js - 更新路由器 View

javascript - Vue过渡动画不播放但保持其动画持续时间

azure - Visual Studio Team Services(原为 Visual Studio Online)构建无法部署 Azure 云服务

linux - Windows Azure Linux VM 缓慢重启

azure - 为什么设置 CosmosDB ThroughputSettings 会导致 "Entity with the specified id does not exist in the system"?

vue.js - 如何处理 vue.js 中的粘贴(Ctrl+v 或鼠标)事件?

node.js - 相同的Azure功能,相同的输入,不同的租户,不同的输出

mvvm - Vue 1.x/2.x : Get vue-router path url from a $route object