Webpack中的导入和要求未正确加载

时间:2017-03-30 14:14:17

标签: javascript webpack vuejs2

我正在使用Vue和Webpack,但是当我导入或者需要将另一个文件包含到我的主文档中时,该文件似乎已经加载了,因为它没有给我任何错误,但是在文件不加载,我不能使用文件中的任何变量。例如,我有这个主要文件:

import './bootstrap';
    import VueRouter from 'vue-router';

    Vue.use(VueRouter);  


    let Default = Vue.component('Default', {

    template: `
                <li>
                    <a href="#">This is home</a>
                </li>
                `
   });


   let About = Vue.component('About', {

    template: `
                <li>
                    <a href="#">This is about us maaan</a>
                </li>
                `
});



var router = new VueRouter({
  routes: [
    { path: '/', component: Default },
    { path: '/about', component: About }
  ]
}) 

require('./sass/styles.scss');

new Vue({
  el: '#app',

  router: router
})

...我的引导程序文件很简单:

import Vue from 'vue';
import axios from 'axios'; 

我收到以下错误:Uncaught ReferenceError: Vue is not defined,好像该文件尚未包含在内。如果我对路径进行任何更改,我会收到一个找不到文件的错误,所以我想象文件是找到的。对于文件中的任何变量都是相同的,之后它们在主文件中不可用。这是我的webpack.config.js:

var path = require('path')
var webpack = require('webpack')

var inProduction = (process.env.NODE_ENV === 'production');

var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: 'dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: 'images/[name].[ext]?[hash]'
        }
      },

      {
          test: /\.s[ac]ss$/,
          use: ExtractTextPlugin.extract({
                use: ['css-loader', 'sass-loader'],
                fallback: 'style-loader'
            })

      }   
    ]
  },

    plugins: [
        new ExtractTextPlugin({
            filename: 'styles.css',
            disable: process.env.NODE_ENV === "development"
        })
  ],

  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    // new webpack.optimize.UglifyJsPlugin({
      // sourceMap: true,
      // compress: {
        // warnings: false
      // }
    // }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

非常感谢!

0 个答案:

没有答案