简体   繁体   中英

InertiaJS with Laravel & Vue2: Cannot redefine property: $inertia

I'm not sure why I am getting this error:

Uncaught TypeError: Cannot redefine property: $inertia
    at Function.defineProperty (<anonymous>)
    at Object.install (app.js:108)
    at Function.Vue.use (app.js:233295)
    at Module../resources/js/app.js (app.js:246829)
    at __webpack_require__ (app.js:20)
    at Object.0 (app.js:253385)
    at __webpack_require__ (app.js:20)
    at app.js:84
    at app.js:87

and this warning:

Registering the Inertia Vue plugin via the "app" component has been deprecated. Use the new "plugin" named export instead.

import { plugin } from '@inertiajs/inertia-vue'

Vue.use(plugin)

I followed the docs as closely as I could. I'm using Vue2 and Laravel 8. I can't find anyone with the same issues anywhere. The error claims that 'App' is no longer used, but the docs for Vue2 use it in their example. I'm confused, what am I missing?

Here is my code:

resources/js/app.js :

import { App, plugin } from '@inertiajs/inertia-vue'
import Vue from 'vue'

Vue.use(App)
Vue.use(plugin)

InertiaProgress.init()

Vue.component('drafts', require('./pages/Drafts').default)

const el = document.getElementById('app')

new Vue({
  render: (h) =>
    h(App, {
      props: {
        initialPage: JSON.parse(el.dataset.page),
        resolveComponent: (name) =>
          import(`./pages/${name}`).then((module) => module.default),
      },
    }),
}).$mount(el)

resources/views/app.blade.php :

<!DOCTYPE html>

<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ config('app.name', 'AppealMaker') }}</title>
    <!-- Styles -->
    <link href="{{ mix('css/app.css') }}" rel="stylesheet">
    <script src="{{ mix('js/app.js') }}"></script>
</head>

<body>
    @inertia
</body>

</html>

web.php :

Route::inertia('/drafts', 'drafts');

I had the same problem.

Here is how your app.js file must look like:

https://inertiajs.com/client-side-setup

summarizing:

  • Remove: Vue.use(App)
  • Change: import( ./pages/${name} ).then((module) => module.default), by require( ./Pages/${name} ).default,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM