javascript - 部署到现在时,Nuxt 应用程序在刷新时出现 404 错误

标签 javascript vue.js axios nuxt.js

当我刷新页面时,我的 nux 应用程序显示 404 未找到错误。如果我通过 nuxt-links 路由浏览 ssr,它工作正常。我做错了什么?

我正在使用“now”命令部署到 now.sh。

我正在使用 slug,我的主要 vue 文件是 _vue.vue 作为动态页面的主要模板。

<template>
<div>
<headerBanner :pagetitle="post[0].title.rendered" :acf_subtitle="post[0].acf.subtitle_text"/>
</div>
</template>

<script>
import axios from 'axios' 
import headerBanner from '~/components/headerbanner.vue'

export default {

  components: {
      headerBanner
  },

  asyncData ({ params, error }) {
    console.log(`https://mywebsite.com/wp-json/wp/v2/pages?slug=${params.vue}`)
    return axios.get(`https://mywebsite.com/wp-json/wp/v2/pages?slug=${params.vue}`)
    .then((res) => {
      return { post: res.data }
    })
    .catch((e) => {
      error({ statusCode: 404, message: 'Post not found' })
    })
  },

  layout: 'default'
}


</script>

现在.json

{
    "version": 2,
    "name": "nuxt-static",
    "builds": [
        { "src": "package.json", "use": "@now/static-build" }
    ]
}

包.json

{
  "name": "myproject",
  "version": "1.0.0",
  "description": "My Nuxt.js project",
  "author": "jordan",
  "private": true,
  "scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate",
    "now-build": "nuxt generate"  
  },
  "dependencies": {
    "@fortawesome/fontawesome": "^1.1.8",
    "@fortawesome/free-solid-svg-icons": "^5.7.2",
    "@nuxtjs/axios": "^5.4.1",
    "@nuxtjs/pwa": "^2.6.0",
    "cross-env": "^5.2.0",
    "nuxt": "^2.4.0",
    "nuxt-fontawesome": "^0.4.0",
    "nuxt-payload-extractor": "^0.0.8",
    "vue-resource": "^1.5.1",
    "vuex": "^3.1.0"
  },
  "devDependencies": {
    "node-sass": "^4.11.0",
    "nodemon": "^1.18.9",
    "sass-loader": "^7.1.0"
  }
}

最佳答案

您将其部署为静态网站。 Nuxt 默认不为动态页面生成路由。

无论如何,您现在就可以使用 nuxt 构建。 https://github.com/nuxt/now-builder

{
  "version": 2,
  "builds": [
    {
      "src": "nuxt.config.js",
      "use": "@nuxtjs/now-builder",
      "config": {}
    }
  ],
  "routes": [
    { "src": "/_nuxt/.+", "headers": { "cache-control": "s-maxage=31536000" } },
    { "src": "/(.*)", "dest": "/" }
  ]
}

关于javascript - 部署到现在时,Nuxt 应用程序在刷新时出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55192784/

相关文章:

javascript - 在 react-admin 中为 View 定义一个可重用的模板

php - Jquery/PHP ajax 表单提交问题

javascript - 为什么 JavaScript eval 函数会抛出 number 属性异常?

javascript - Vue js 条件布局

javascript - '错误: Please install pg package manually' in js console after running npm run serve

javascript - 主干 POST JSON

javascript - 为什么当我选择第二个输入框时音译不起作用?

javascript - react + Redux : Why are props populated on third time load?

ajax - 带有 auth 的 Laravel api 路由

javascript - 如何获取数据并存储在 React Context API 中?