Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 导入工作正常,但为什么需要不工作?_Javascript_Webpack_Babeljs - Fatal编程技术网

Javascript 导入工作正常,但为什么需要不工作?

Javascript 导入工作正常,但为什么需要不工作?,javascript,webpack,babeljs,Javascript,Webpack,Babeljs,我正在使用webpack在main.js中构建一个简单的项目。如果我使用import导入app.js,结果很好。但是,如果使用require,vue模板将不会显示在页面上 我以为巴贝尔会将导入编译成需要;如果是,为什么require在这里不起作用 使用结果要求: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport

我正在使用webpack在
main.js
中构建一个简单的项目。如果我使用
import
导入
app.js
,结果很好。但是,如果使用
require
,vue模板将不会显示在页面上

我以为巴贝尔会将
导入
编译成
需要
;如果是,为什么
require
在这里不起作用

使用结果要求:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>  
</head>
<body>
    <!--function(e,n,r,o){return Fe(t,e,n,r,o,!0)}-->
    <h1>123</h1>
</body>
</html>
webpack.config.js

const htmlPlugin = require('html-webpack-plugin')


module.exports = {
  entry: path.join(__dirname, "./src/main.js"),

  output: {
    path: path.join(__dirname, "./dist"),
    filename: "bundle.js"
  },

  module: {
    rules: [
      { test: /\.js$/, use: "babel-loader", exclude: /node_modules/ },
      { test: /\.vue$/, use: "vue-loader" }
    ]
  },
  plugins: [
    new htmlPlugin({
      minify: {
        removeAttributeQuotes: true
      },
      hash: true,
      template: "./src/index.html"
    })
  ],
  resolve: {
    // extensions: [ '.vue'],
    alias: {
      'vue$': 'vue/dist/vue.common.js'
    }
  },
};
app.js

<template>
<div class="app-contianer">
    <h1>test</h1>
</div>
</template>
<script>
export default {
  data () {
    return {
    };
  }
}
</script>
<style lang="css" scoped>
</style>

测试
导出默认值{
数据(){
返回{
};
}
}

require
import
访问导入对象的方式不同

import app from './app.vue'
这一行实际上意味着这一点,它的工作原理应该与导入相同:

const app = require('./app.vue').default

为完整起见,此行:

const app = require('./app.vue')
相当于:

import * as app from './app.vue'

require
import
访问导入对象的方式不同

import app from './app.vue'
这一行实际上意味着这一点,它的工作原理应该与导入相同:

const app = require('./app.vue').default

为完整起见,此行:

const app = require('./app.vue')
相当于:

import * as app from './app.vue'

最后一个文件是*app.vue最后一个文件是*app.vue