Javascript 导入工作正常,但为什么需要不工作?
我正在使用webpack在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
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