laravel - 获取 _ctx.route 不是一个函数

标签 laravel vuejs3 inertiajs

我试图通过在不使用 @routes 的情况下导入 laravel inertiajs 应用程序(VUE3)来使用 ziggy,但我无法使用例如。 route('home').getting ctx.route 不是函数错误.. 请告诉我如何正确放置...
应用程序.js

        require('./bootstrap');

        // Import modules...
        import { createApp, h } from 'vue';
        import { App as InertiaApp, plugin as InertiaPlugin } from '@inertiajs/inertia-vue3';
        import { InertiaProgress } from '@inertiajs/progress';
        import { Link } from '@inertiajs/inertia-vue3'
        import { ZiggyVue } from 'ziggy';
        import { Ziggy } from './ziggy';





        const el = document.getElementById('app');

        createApp({
            render: () =>
                h(InertiaApp, {
                    initialPage: JSON.parse(el.dataset.page),
                    resolveComponent: (name) => require(`./Pages/${name}`).default,
                }),
        })
        .use(InertiaPlugin, Link, ZiggyVue, Ziggy)
        // .mixin({ methods: { route } })
            .mount(el);

        InertiaProgress.init({ color: '#4B5563' });
webpack.mix.js
    const mix = require('laravel-mix');
    const path = require('path');

    /*
    |--------------------------------------------------------------------------
    | Mix Asset Management
    |--------------------------------------------------------------------------
    |
    | Mix provides a clean, fluent API for defining some Webpack build steps
    | for your Laravel applications. By default, we are compiling the CSS
    | file for the application as well as bundling up all the JS files.
    |
    */

    mix.js('resources/js/app.js', 'public/js').vue()
        .postCss('resources/css/app.css', 'public/css', [
            require('postcss-import'),
            require('tailwindcss'),
        ]).alias({
            ziggy: path.resolve('vendor/tightenco/ziggy/dist/vue'), // or 'vendor/tightenco/ziggy/dist/vue' if you're using the Vue plugin
        })
        .webpackConfig(require('./webpack.config'));

    if (mix.inProduction()) {
        mix.version();
    }
索引.vue
    <template>
        <div class="text-blue-800">
            <Link :href="route('about')">ABOUT</Link>
            <div>HALO</div>
        </div>
    </template>

    <script>
        import { Link } from '@inertiajs/inertia-vue3'
        export default {
            components: {
                Link
            }
        }
    </script>

Error I am getting is.

    _ctx.route is not a function
请有人帮助我正确地做...
我不想在主 Blade 文件上使用@routes

最佳答案

首先在终端中运行以下命令

npm i ziggy-js
然后将以下内容复制到您的 app.js 文件中
import {createApp, h} from 'vue'
import {createInertiaApp} from '@inertiajs/inertia-vue3'
import {InertiaProgress} from '@inertiajs/progress'
import route from "ziggy-js";

const el = document.getElementById('app');

createInertiaApp({
    resolve: name => import(`./Pages/${name}`),
    setup({el, App, props, plugin}) {
        createApp({
            render: () => h(App, props)
        })
        .mixin({ methods: { route } })
        .use(plugin)
        .mount(el)
    },
})

InertiaProgress.init({ color: '#4B5563' });
确保从 'ziggy-js' 导入路由,如图所示

关于laravel - 获取 _ctx.route 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68473240/

相关文章:

php - 使用 Laravel ORM 的较大查询中的 SQL Union 1 列?

mysql - Laravel 迁移 - 违反完整性约束 : 1452 Cannot add or update a child row: a foreign key constraint fails

session - 在 Laravel 中,在 session 中传递不同类型的 flash 消息的最佳方式

json - Vue3 : Using dynamically imported JSON in template

php - Laravel Eloquent 多态模型

vue.js - 使用 scss 更改默认 Vuetify 3 组件设置不起作用?

unit-testing - 如何在 vitest、vue/test-utils vue3 中对 vueuse/head 进行单元测试

javascript - 如何在 vue.js 欢迎组件中使用 Bootstrap 而不是尾风 CSS

laravel - Inertia 动态添加项目到 useForm()

laravel - 如何使用惯性 js 保留滚动按钮