laravel10 vue3 inertia开发方式, <Link>组件无法打开页面

1. 运行环境

1). 当前使用的 Laravel 版本?

10.48.29

2). 当前使用的 php/php-fpm 版本?

PHP 版本:8.2.9
php-fpm 版本:apache, 未使用php-fpm

3). 当前系统

win10

4). 业务环境

本地开发环境

5). 相关软件版本

node: v18.15.0
npm: 9.5.0

2. 问题描述?

两个页面均由return Inertia::render('Finance/Device/Income');渲染,在浏览器中可以直接打开;但使用Link组件互相跳转时,前端js报错,Uncaught (in promise) TypeError: this.response.headers is undefined
点击Link组件时,系统实际是一个ajax请求,返回json,
laravel10 vue3 inertia开发方式, <Link>组件无法打开页面
返回头信息中有inertia的信息
laravel10 vue3 inertia开发方式, <Link>组件无法打开页面
package.json

{
    "private": true,
    "type": "module",
    "scripts": {
        "dev": "vite",
        "watch": "vite build --watch",
        "build": "vite build"
    },
    "devDependencies": {
        "@inertiajs/vue3": "^2.0.0",
        "@tailwindcss/forms": "^0.5.3",
        "@vitejs/plugin-vue": "^5.0.0",
        "autoprefixer": "^10.4.12",
        "axios": "^1.7.4",
        "concurrently": "^9.0.1",
        "laravel-vite-plugin": "^1.0",
        "postcss": "^8.4.31",
        "tailwindcss": "^3.2.1",
        "vite": "^6.0",
        "vue": "^3.4.0"
    },
    "dependencies": {
        "@element-plus/icons-vue": "^2.3.1",
        "@inertiajs/inertia": "^0.11.1",
        "element-plus": "^2.9.3",
        "lodash": "^4.17.21"
    }
}

vue3

import '../css/app.css';
import './bootstrap';

import { createInertiaApp } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { createApp, h } from 'vue';
import { ZiggyVue } from '../../vendor/tightenco/ziggy';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const appName = import.meta.env.VITE_APP_NAME || '';         // 对应本地.env文件中的VITE_APP_NAME, VITE_APP_NAME又对应APP_NAME值

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    // router,
    resolve: (name) =>
        resolvePageComponent(
            `./Pages/${name}.vue`,
            import.meta.glob('./Pages/**/*.vue'),
        ),
    setup({ el, App, props, plugin }) {
        const app = createApp({ render: () => h(App, props) })
            .use(plugin)
            .use(ElementPlus)
            .use(ZiggyVue);
        for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
            app.component(key, component)
        }
        app.mount(el);
        return app;
    },
    progress: {
        color: '#4B5563',
    },
});

bootstrap.js

import axios from "axios"
import { ElMessage  } from 'element-plus';
import _ from 'lodash';
window._ = _;

window.token = document.head.querySelector('meta[name="csrf-token"]');
if (window.token) {
    axios.defaults.headers.common['X-CSRF-TOKEN'] = window.token.content;
    axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

axios.interceptors.response.use(
    response => {
        if (response.data.respCode > 0) {
            if (response.data.msgBox == 0) {
                ElMessage.error('错误:' + response.data.respDesc + (response.data.msg ? ' ' + response.data.msg : ''));
            }

            return Promise.reject(response.data.respDesc);
        }

        return response.data;
    },
    error => {
        ElMessage.error('服务器异常:' + error);
        return Promise.reject(error)
    }
);
window.ajax = axios;

3. 您期望得到的结果?

使用link组件实现单页面跳转

4. 您实际得到的结果?

js报错Uncaught (in promise) TypeError: this.response.headers is undefined,页面无法单页面跳转

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

axios 不要改全局的,创建一个实例。

const ajax = axios.create(....)
ajax.interceptors.response.use(....)
window.ajax = ajax

试试这样,应该是这个问题,之前被这个问题困扰了好几天,好像是 inertia 会使用 axios 如果通过全局配置了响应拦截,就会有问题。

2周前 评论
meditatorzhang (楼主) 6天前
讨论数量: 2

axios 不要改全局的,创建一个实例。

const ajax = axios.create(....)
ajax.interceptors.response.use(....)
window.ajax = ajax

试试这样,应该是这个问题,之前被这个问题困扰了好几天,好像是 inertia 会使用 axios 如果通过全局配置了响应拦截,就会有问题。

2周前 评论
meditatorzhang (楼主) 6天前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!