简体   繁体   中英

Vue.js Codes Not Work With Webpack

I'm Using Webpack And Vue.js. I have Very Simple Code Just Like This :

<div id="msg">
    <h1>{{msg}}</h1>
</div>

new Vue({
   el: '#msg',
   data:{
       msg:'Hello'
   }
});

If I Use vue.js Directly at Index.html , Then My Code Work Fine But If I Import vue.js In Seprate js File Then I Got Error :

Uncaught ReferenceError: Vue is not defined at index.html?_ijt=e0ucco3tsm7no18s2or5votcio:305

Import vue.js In Seprate js File Like This :

import Vue from 'vue';

And At My Console I can See That Vue Loaded With No Problem :

You are running Vue in development mode. Make sure to turn on production mode when deploying for production. See more tips at https://vuejs.org/guide/deployment.html

I Wonder Where is My Problem? Why My Code Not Running? And Also I Have To Say I'm Using index.js File For Load My All Styles & JS Codes.

Here Some More Info :

"devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-preset-es2017": "^6.24.1", "cross-env": "^5.1.3", "css-loader": "^0.28.8", "extract-text-webpack-plugin": "^3.0.2", "file-loader": "^1.1.6", "jshint": "^2.9.5", "jshint-loader": "^0.8.4", "lodash": "^4.17.4", "node-sass": "^4.7.2", "sass-loader": "^6.0.6", "style-loader": "^0.19.1", "vue": "^2.5.13", "vue-awesome-swiper": "^3.1.0", "vue-cli": "^2.9.2", "webpack": "^3.10.0" },

const webpack = require('webpack');
const paths = require('path');
module.exports = {
entry: './js/index.js',
output: {
    filename: 'shop.js',
    path: paths.resolve(__dirname, 'files/js')
},
module: {
    rules: [
        {
            test:/\*.vue$/,
            loader: 'vue-loader'
        },

        /* Babel For ES 6*/
        {
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                    "presets": ["es2017"]
                }
            }
        },
        //Sass Loader
        {
            test: /\.s[ac]ss$/,
            use: ['style-loader', 'css-loader', 'sass-loader']
        },
        //File Loader For Fonts
        {
            test: /\.(eot|svg|ttf|woff|woff2)$/,
            use: [
                {
                    loader: 'file-loader',
                    options: {
                        name:`[path][name].[ext]`,
                    }
                }
            ]
        },
        //File Loader For IMG
        {
            test: /\.(jpg|jpeg|png|gif)$/,
            use: [
                {
                    loader: 'file-loader',
                    options: {
                        name:`[path][name].[ext]`,
                    }
                }
            ]
        }
    ]
  }
 };

Your dev dependencies don't seem to have vue-loader in there. You have registered the .vue file to #msg. Try registering globally to see if you need to reconsider how you are registering them.

To register a global component, you can use Vue.component(tagName, options). For example:

Vue.component('my-component', {
  // options
})

Additionally, where you're using vue loader in web pack you may want to use es module options as below:

{
    test: /\.vue$/,
    loader: 'vue-loader',
    options: {
        esModule: true,
        extractCSS: true
    }
}

One tip I will give, which helped me with vue.js development is to separate your html, js and css into their own files and inject them into a vue file. this will make it easier for you to inject/import your js files into other files without causing issues with loaders.

use the official tools to start the project

vue-cli

or

vuejs-templates

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