webpack vue.js

时间:2018-06-07 14:14:13

标签: javascript webpack vue.js

我尝试用vuejs和webpack构建一个项目。

这是我的index.js

import Vue from 'vue'
import App from './app.vue'

console.log(document.body)

const root = document.createElement('div')
document.body.appendChild(root)
new Vue({
    render:(h) => h(App)
}).$mount(root)

将webpack打包到bundles.js后,在浏览器上运行它,它会上升: enter image description here

如您所见,将beacames记录为null。

为什么文档不起作用?这个bug是我的问题还是来自vue.js?

这是我的webpack.config.js:

const path = require("path")

module.exports = {
    entry:path.join(__dirname,"./src/index.js"),
    output: {
        path: path.join(__dirname,"dist"),
        filename: "bundle.js"
    },
    module:{
        rules:[
            {
                test: /\.vue$/,
                loader:"vue-loader"
            },
            {
                test:/\.css$/,
                use:[
                    'style-loader',
                    "css-loader"
                ]
            },
            {
                test:/\.(gif|jpg|jpeg|png|svg)$/,
                use:[
                    {
                        loader: "url-loader",
                        options: {
                            "limit":1024,
                            name:"[name]-aaa.[ext]"
                        }
                    }
                ]
            }
        ]
    }
}

这是package.json

{
  "description": "todolist",
  "license": "MIT",
  "scripts": {
    "build": "webpack --config webpack.config.js"
  },
  "dependencies": {
    "vue": "2.5.16"
  },
  "devDependencies": {
    "webpack-cli": "^3.0.3",
    "webpack": "^4.11.1",
    "css-loader": "^0.28.11",
    "file-loader": "^1.1.11",
    "style-loader": "^0.21.0",
    "url-loader": "^1.0.1",
    "vue-loader": "^14.2.2",/*15. 版本不行*/
    "vue-template-compiler": "^2.5.16"
  }
}

1 个答案:

答案 0 :(得分:2)

我认为您的index.js位于HTML的头部,并且由于JavaScript资源阻塞,您的身体尚未定义。

您需要将JavaScript放在要定位的元素之后,或者需要使用onload事件。

https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload