@route在app.blade.php中设置ziggy后无法被识别



即使在设置Ziggy并运行npm run watch和/或npm run production后,@route指令仍然保持原样,只是作为普通字符串处理。

<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<link href="/css/app.css" rel="stylesheet" />
@routes
<script src="/js/app.js" defer></script>
</head>

假设,@routes应该转换为<?php echo app('TightencoZiggyBladeRouteGenerator')->generate(); ?>(基于从https://inertiajs.com/demo-application下载的InertiaJS演示应用程序的源代码),但由于某种原因,它保持这样。

以下是我的代码:

webpack.mix.js

const mix = require('laravel-mix');
const path = require('path');
mix
.js('resources/js/app.js', 'public/js').vue()
.sass('resources/css/app.scss', 'public/css')
.webpackConfig(
{
output: {
chunkFilename: 'js/[name].js?id=[chunkhash]',
}
}
).alias({
ziggy: path.resolve('vendor/tightenco/ziggy/dist/vue'),
});

app.blade.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<link href="{{ mix('/css/app.css') }}" rel="stylesheet" />
@routes
<script src="{{ mix('/js/app.js') }}" defer></script>
</head>
<body>
@inertia
</body>
</html>

app.js

import {createApp, h} from 'vue'
import {createInertiaApp} from '@inertiajs/inertia-vue3'
import {InertiaProgress} from '@inertiajs/progress'
import { ZiggyVue } from 'ziggy'
import { Ziggy } from './ziggy'
InertiaProgress.init()
createInertiaApp({
resolve: name => require(`./Pages/${name}`),
setup({el, App, props, plugin}) {
const app = createApp({render: () => h(App, props)})
.use(plugin, ZiggyVue, Ziggy)
.mixin({ methods: { route: window.route } });
app.config.globalProperties.$route = (a, b = {}) => {
const token = new URLSearchParams(window.location.search).get('t');
b["t"] = token;
route(a, b)
};
// app.config.globalProperties.$token = token;
app.mount(el);
},
})

composer.json

{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"inertiajs/inertia-laravel": "^0.4.4",
"laravel/framework": "^8.54",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5",
"tightenco/ziggy": "^1.4"
},
"require-dev": {
"facade/ignition": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3.3"
},
"autoload": {
"psr-4": {
"App\": "app/",
"Database\Factories\": "database/factories/",
"Database\Seeders\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\Foundation\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi"
],
"post-root-package-install": [
"@php -r "file_exists('.env') || copy('.env.example', '.env');""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}

我按照https://inertiajs.com/server-side-setup的步骤设置中间件,并更改默认的app.blade.php以包含@inertia指令

感谢你的帮助!

ok,所以我检查了InertiaJS的样例CRM项目,并查看了生成的app.blade.php文件,@routes<?php echo app('TightencoZiggyBladeRouteGenerator')->generate(); ?>替换了。我取代了@routesroute()功能的工作现在。如果有更好的方法,我会很感激的。

@route()代替@route解决了问题

如果@routes不起作用,可以使用@routes()。它会为你呈现javascript标签

相关内容

  • 没有找到相关文章

最新更新