Uncaught TypeError: Cannot set property 'innerHTML' of null

来源:4-6 处理模板文件

sun_haha

2017-04-13 14:11

var path = require("path");
var webpack = require("webpack");
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        app: './src/app.js',
    },
    output: {
        filename: 'js/[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module:{
        rules:[{
                test:/\.js$/,
                include:path.resolve(__dirname,'src'),
                exclude:path.resolve(__dirname,'node_modules'),
                loader: 'babel-loader?presets[]=env',
            },{
                test:/\.html$/,
                use:['html-loader']
            },{
                test:/\.tpl$/,
                use:['ejs-loader']
            },{
                test:/\.css$/,
                use:[ 'style-loader', 
                      {
                          loader:'css-loader',
                          options:{
                              importLoaders:1
                          }
                      },
                    'postcss-loader']
            },{
                test:/\.less$/,
                use:['style-loader','css-loader', 'postcss-loader', 'less-loader']
            },{
                test:/\.scss$/,
                use:['style-loader','css-loader', 'postcss-loader', 'sass-loader']
            },{
                 test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                 use: ['file-loader']
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
            inject: 'body'
        }),
        new webpack.LoaderOptionsPlugin({
            options : {
                postcss : function(){
                    return [
                        require('autoprefixer')({
                            browsers: ['ie>=8','>1% in CN']
                        })
                    ];
                }
            }
        })
    ]
}
//layer.js

import './layer.less';
import mytpl from './layer.tpl';


function layer(){
    return {
        name:'layer',
        tpl: mytpl
    };
}

export default layer;
//app.js

import './app/css/common.css';

import Layer from './app/components/layer/layer.js';

const App = function(){
    var dom = document.getElementById('#app');
    var layer = new Layer();

    dom.innerHTML = layer.tpl({
        name:'sunxq',
        arr:['zhangsan','lisi','wangwu']
    });
}

new App();

http://img.mukewang.com/58ef167c000193b908740367.jpg

写回答 关注

1回答

  • 街角咖啡
    2017-04-13 17:26:31
    已采纳
     var dom = document.getElementById('#app');

    去掉 "#app" 之前的#号就可以了

    id名 是app。

    sun_ha...

    我竟然没发现 看了大半天 谢谢o.o!

    2017-04-14 11:11:08

    共 1 条回复 >

webpack深入与实战

webpack实战教程,用真实项目带你探索 webpack 强大的功能

86555 学习 · 721 问题

查看课程

相似问题