开发者社区> 问答> 正文

vue+webpack遇到错误TypeError: this._init is not a function

我写了一个vue.js+webpack的例子,在进行webpack命令时出现了如下错误。
screenshot

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>webpack vue</title>
    <style media="screen">
      #app {
        margin: 20px auto;
        width: 800px;
        height: 600px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h1>Hell World!</h1>
      <p>
        <a v-link='{path: '/foo'}'>go to foo</a>
        <a v-link='{path: '/bar'}'>go to bar</a>
      </p>

      <router-view></router-view>
    </div>
    <script src="dist/vue.js" charset="utf-8"></script>
    <script type="text/javascript" src="dist/main.js"></script>
  </body>
</html>
main.js:

import Vue from '../node_modules/vue';
import VueRouter from '../node_modules/vue-router'

Vue.use(VueRouter);

import index from './components/app';
import list from './components/list';
import hello from './components/hello';

Vue.config.debug = true;

var App = Vue.extend({});

var Router = new VueRouter();

router.map({
  '/index': {
    name: 'index',
    component: index,
    subRoutes: {
      '/hello': {
        name: 'hello',
        component: hello
      }
    }
  },
  '/list': {
    name: 'list',
    component: list
  }
});

router.redirect({
  '*': '/index'
});

router.start(App, '#app');
webpack.config.js :

var path = require('path');

module.exports = {
  entry: './src/main',
  output: {
    path: path.join(__dirname, './dist'),
    filename: '[name].js',
    publicPath: '/dist/'
  },
  devServer: {
    historyApiFallback: true,
    hot: false,
    inline: true,
    grogress: true
  },
  module: {
    loaders: [
      { test: /\.vue$/, loader: 'vue' },
      { test: /\.js$/, loader: 'babel', exclude: /node_modules/ },
      { test: /\.css$/, loader: 'style!css!autoprefixer' },
      { test: /\.scss$/, loader: 'style!css!sass?sourceMap' },
      { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192' },
      { test: /\.(html|tpl)$/, loader: 'html-loader' }
    ]
  },
  vue: {
    loaders: {
      css: 'style!css!autoprefixer!sass',
    }
  },
  babel: {
    presets: ['es2015'],
    plugins: ['transform-runtime']
  },
  resolve: {
    extensions: ['', '.js', '.vue'],
    alias: {
      filter: path.join(__dirname, './src/filters'),
      components: path.join(__dirname, './src/components')
    }
  },
  devtool: 'eval-source-map'
};
app.vue:

<script>
  export default {
    data () {
      return {
        name: 'chen',
        age: '21'
      }
    },
    method: {
      golist () {
        this.$route.router.go({name: 'list'});
      }
    }
  }
</script>

<template>
  <div>
    <h1>姓名:{{name}}</h1>
    <h2>{{age}}</h2>
    <button @click="golist">$route.router.go查看</button>
    <a v-link="{ name: 'list' }">v-link查看列表</a>
    <a v-link="{ name: 'index' }">回去主页</a>
  </div>
</template>

<style lang='sass'>
  $q:#098;
  body{
    background-color: $q;
    h1{
      background-color: #eee;
      color: red;
      transform: translate(10%, 20%);
      &:hover{
        height: 100px;
      }
    }
    h2{
      background-color: #999;
    }
  }
</style>
是哪里没注意到吗?

展开
收起
a123456678 2016-03-13 11:32:51 10510 0
1 条回答
写回答
取消 提交回答
  • router.start(App, '#app');
    话说你把App这个组件挂载到#app上,这样页面里原来的部分还不在?我感觉好像应该是不在的吧

     <div id="app">
      <h1>Hell World!</h1>
      <p>
        <a v-link='{path: '/foo'}'>go to foo</a>
        <a v-link='{path: '/bar'}'>go to bar</a>
      </p>
    
      <router-view></router-view>
    </div>
    2019-07-17 19:02:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
基于webpack和npm的前端组件化实践 立即下载
Vue.js 在前端服务化上的探索与实践 立即下载
Vue.js在前端服务化上的实践与探索 立即下载