未捕获的类型错误:Vue.component 不是函数

Ste*_*pas 14 javascript laravel console.log homestead vuejs2

在带有 Laravel/Homestead 的 OS X 中,我在使用 Vue (v2.2.1) 时遇到错误。组件不会加载,控制台错误是“Uncaught TypeError: Vue.component is not a function”。

完整的控制台错误

    Uncaught TypeError: Vue.component is not a function
        at eval (eval at <anonymous> (app.js:321), <anonymous>:17:5)
        at Object.<anonymous> (app.js:321)
        at __webpack_require__ (app.js:20)
        at app.js:64
        at app.js:67  
Run Code Online (Sandbox Code Playgroud)

让我失望的是,如果我对 app.js 进行更改,webpack 不会更新以反映任何更改。所以我需要 A) 解决上面的问题,B) 弄清楚如何让 webpack 在控制台中显示更新。

任何帮助将不胜感激!我是菜鸟。这是我的代码...

app.js 文件

    Vue.component('video-upload', require('./components/VideoUpload.vue'));

    const app = new Vue({
        el: 'body'
    });
Run Code Online (Sandbox Code Playgroud)

视频上传.vue

    <template>
        <div class="container">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">
                        <div class="panel-heading">Example</div>

                        <div class="panel-body">
                            This is an example
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </template>

    <script>
        export default {
            mounted() {
                console.log('Component mounted.')
            }
        }
    </script>
Run Code Online (Sandbox Code Playgroud)

上传刀片.php

    @extends('layouts.app')

    @section('content')
        <video-upload></video-upload>
    @endsection
Run Code Online (Sandbox Code Playgroud)

包.json

    {
      "private": true,
      "scripts": {
        "prod": "gulp --production",
        "dev": "gulp watch"
      },
        "devDependencies": {
        "bootstrap-sass": "^3.3.7",
        "gulp": "^3.9.1",
        "jquery": "^3.1.0",
        "laravel-elixir": "^6.0.0-9",
        "laravel-elixir-vue": "^0.1.4",
        "laravel-elixir-webpack-official": "^1.0.2",
        "lodash": "^4.14.0",
        "video.js": "^5.11.6",
        "vue": "^2.2.1",
        "vue-resource": "^0.9.3"
      }
    }
Run Code Online (Sandbox Code Playgroud)

小智 28

在laravel 7 项目上从laravel-mix 5.0.9to更新后,laravel-mix 6.0.11它开始在 vue 编译视图上看到此错误。我改变调用Vue包:

使用import Vue from 'vue'而不是window.Vue = require("vue");为我工作。


Saf*_*thi 15

对于任何有同样问题且与 Laravel 无关的人。

提示:使用 Vue 3+

全局注册组件:

const MyComponent = {
  // ... options ...
}

// if you do not have App component
Vue.createApp({}).component("my-component", MyComponent).mount("#app");

// if you have an App component
const App = {
  // ... options ...
}
const app = Vue.createApp(App);
app.component("my-component", MyComponent);
app.mount("#app");
Run Code Online (Sandbox Code Playgroud)

请参阅文档以获取更多信息


小智 11

使用 import 关键字在您的代码中正确导入 vue,如下所示:

//import vue
import Vue from 'vue';

//register component
Vue.component('yourComponentName',require('./components/yourComponentName.vue').default);

//initialize vue
const app = new Vue({
    el: '#app',
});
Run Code Online (Sandbox Code Playgroud)


Lar*_*low 4

1

更改你的package.json

"laravel-elixir-vue-2": "^0.3.0"
Run Code Online (Sandbox Code Playgroud)

并运行

<!-- language: lang-js -->
npm install
Run Code Online (Sandbox Code Playgroud)

或者

2

npm install laravel-elixir-vue-2 --save-dev
Run Code Online (Sandbox Code Playgroud)

进而

像这样更改你的 gulpfile.js

<!-- language: lang-js -->
var elixir = require('laravel-elixir')

require('laravel-elixir-vue-2');
Run Code Online (Sandbox Code Playgroud)