鍍金池/ 問答/HTML/ webpack打包vue項(xiàng)目,vue的template內(nèi)容沒顯示出來

webpack打包vue項(xiàng)目,vue的template內(nèi)容沒顯示出來

剛剛開始學(xué)習(xí)webpack+vue的項(xiàng)目開發(fā),配置好之后用webpack-dev-server運(yùn)行,但是頁面什么都沒有,也沒報(bào)錯(cuò)。找了兩天還是不知道什么原因。

下面是代碼

package.json

{
  "name": "webpack_project2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --open --config webpack.config.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "vue": "^2.5.16"
  },
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-runtime": "^6.26.0",
    "extract-text-webpack-plugin": "^3.0.2",
    "vue-hot-reload-api": "^2.3.0",
    "vue-loader": "^14.2.2",
    "vue-style-loader": "^4.1.0",
    "vue-template-compiler": "^2.5.16",
    "webpack": "^4.4.1",
    "webpack-cli": "^2.0.13",
    "webpack-dev-server": "^3.1.1"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./dist/main.bundle.js"></script>
</head>
<body>
    <div id="app"></div>
</body>
</html>

main.js

import Vue from 'vue';
new Vue({
    el:'#app',
    data:{
        hello:'hello world'
    },
    beforeCreate:function(){
        console.log("beforeCreate")
    },
    created:function(){
        console.log("created")
    },
    beforeMount:function(){
        console.log("beforeMount")
    },
    mounted:function(){
        console.log(this.hello)
    },
    beforeUpdate:function(){
        console.log("beforeUpdate")
    },
    updated:function(){
        console.log("updated")
    },
    template:
        `
        <div>
            <p>{{hello}}</p>
        </div>
        `
})
console.log('main.js正在運(yùn)行')

webpack.config.js

var path = require('path');

var config = {
    entry:{
        main:'./main.js'
    },
    output:{
        path:path.join(__dirname,'./dist'),
        publicPath:'/dist/',
        filename:'[name].bundle.js'
    },
    module:{
        rules:[
            {
                test:/\.vue$/,
                loader:'vue-loader',
            },
            {
                test:/\.js$/,
                loader:'babel-loader',
                exclude:/node_modules/
            },
            {
                test:/\.css$/,
                loader: "css-loader!style-loader"
            }
        ]
    }
};

module.exports = config;

運(yùn)行結(jié)果
頁面上一片白板,什么都沒有,控制臺(tái)沒有報(bào)錯(cuò)
圖片描述

圖片描述

回答
編輯回答
久愛她
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./dist/main.bundle.js"></script>
    // 執(zhí)行上面的js的時(shí)候能找到 #app 嗎 ? 
    // 嘗試把這段代碼挪到 #app后面
</head>
<body>
    <div id="app"></div>
</body>
</html>
2018年1月26日 06:18