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,
返回头信息中有inertia的信息
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
,页面无法单页面跳转
推荐文章: