我在用vuejs的webpack.Webpack有自己的功能,但当我看到输出的应用程序时.js文件,它给了我这个错误.

"导入"和"导出"只能出现在顶层

我想这是因为巴贝尔没有转换代码?因为我在浏览应用程序时会在浏览器中看到这个.

意外的令牌导入

Here's my entry.js for my vuejs application:

/*jshint esversion: 6 */
import Vue from 'vue';
import App from './App.vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
require('./css/style.scss');

// Export the vue router
export var router = new VueRouter({
  hashbang: false,
  root: '/'
  // history: true
});

// Set up routing and match routes to components
router.map({
  '/': {
    component: require('./components/home/Home.vue')
  }
});

// Redirect to the home route if any routes are unmatched
router.redirect({
  '*': '/'
});

// Start the app on the #app div
router.start(App, '#app');

Here's my webpack.config.js:

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

module.exports = {
  entry: './src/entry.js',
  output: {
      filename: './public/js/app.js'
  },
  devtool: 'source-map',
  plugins: [
    new ExtractTextPlugin('./public/css/style.css')
  ],
  module: {
    preLoaders: [{
        test: /\.js$/, 
        exclude: /node_modules/,
        loader: 'jshint-loader'
    }],
    loaders: [{
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract(
            'style',
            'css!sass'
        ),
    },
    {
        test: /\.vue$/,
        loader: 'vue'
    },
    {
        test: /\.js$/,
        exclude: /node_modules/,
        include: [
          path.resolve(__dirname, "src/services"),
        ],
        loader: 'babel-loader',
        query: {
          presets: ['es2015']
        }
    }]
  }
};

这是我的Package.json文件:

{
  "name": "test-webpack",
  "version": "1.0.0",
  "description": "Myapp",
  "main": "entry.js",
  "scripts": {
    "watch": "webpack-dev-server  --host $IP --port $PORT  --hot --inline --config webpack.config.js",
    "dev": "webpack",
    "build": ""
  },
  "author": "Dev",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.9.1",
    "babel-loader": "^6.2.4",
    "babel-plugin-transform-class-properties": "^6.10.2",
    "babel-plugin-transform-runtime": "^6.9.0",
    "babel-preset-es2015": "^6.9.0",
    "babel-runtime": "^6.9.2",
    "css-loader": "^0.23.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "jshint": "^2.9.2",
    "jshint-loader": "^0.8.3",
    "node-sass": "^3.8.0",
    "path": "^0.12.7",
    "sass-loader": "^3.2.0",
    "style-loader": "^0.13.1",
    "vue-hot-reload-api": "^1.3.2",
    "vue-html-loader": "^1.2.2",
    "vue-loader": "^8.5.2",
    "vue-style-loader": "^1.0.0",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": {
    "vue": "^1.0.25",
    "vue-router": "^0.7.13"
  }
}

推荐答案

当我缺少一个结束括号时,我犯了这个错误.

简化娱乐:

const foo = () => {
  return (
    'bar'
  );
}; <== this bracket was missing

export default foo;

Vue.js相关问答推荐

如何修复IntersectObserver导致我的OpenLayers Web应用程序中内存泄漏的问题?

可组合数据获取示例

在预渲染模式下部署 nuxt 3 时出错

使用项目和字段,列跨度为 2 的 Bootstrap-vue 表

在 bootstrap-vue 中渲染自定义样式

在重复表行中

使用 Vue Js 运行 Jest 测试时出现语法错误:无法在模块外使用 import 语句

v-model 不会检测到 jQuery 触发事件所做的更改

如何使用 vuejs 从 SPA 访问 /storage -laravel- 中的图像

全局禁用 vuetify 组件的涟漪效应

包含 Vue 组件的 HTML 字符串的 Nuxt 渲染函数

为什么 v-model 不适用于数组和 v-for 循环?

了解 Nuxt.js 中的State状态和Getters:Getters不起作用

如何通过 Axios 发送文件到 Laravel

如果我刷新我的网页,Vuex store是空的

何时使用

NPM:403 禁止 - PUT http://registry.npmjs.org/[package-name] - 禁止

Vue js在列表中添加动态字段,删除和排序不起作用

如何使用 Vuetify 验证复选框组

使用 Vuetify 的可滚动数据表