Vue.js "npm run build" 但 Vue.js 未绑定到 DOM / 工作

Vue.js "npm run build" but Vue.js not bound to DOM / working

这里是 Vue.js 的新手。在 Mac OS 使用版本:

$ npm --version
4.6.1
$ vue --version
2.8.1

我正在使用 webpack-simple init 和 vue-cli for vue 2.0。我在我的 Django 项目文件夹中为 vue 东西创建了一个名为 frontend 的文件夹。目录结构:

$ tree 
├── README.md
├── asnew
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── settings.py
│   ├── templates
│       └── index.html
│   ├── urls.py
│   ├── views.py
│   ├── wsgi.py
├── frontend
│   ├── node_modules
│   ├── package.json
│   ├── src
│       ├── App.vue
│       ├── assets
│       ├── components
│       │   └── SearchPageResult.vue
│       ├── main.js
│       └── webpack.config.js
├── manage.py
├── media
├── requirements.txt
├── static
└── staticfiles

然后基本上在我的 index.html Django 模板中我有以下代码:

<script src="{% static 'js/vue/build.js' %}"></script>
<div id="app"></div>

渲染后,这将变成完整路径:

<script src="/static/js/vue/build.js"></script>
我用 npm run build 创建的

并且我验证过浏览器确实得到 loaded/imported 。我 运行 heroku CLI 作为开发服务器。

我是这样建造的:

$ cd frontend
$ npm run build

> vue-asnew@1.0.0 build /Users/me/MyProject/frontend
> cross-env NODE_ENV=production webpack --progress --hide-modules

Hash: d5e16854b8f88beea3e9
Version: webpack 2.4.1
Time: 4503ms
       Asset     Size  Chunks             Chunk Names
    build.js  87.4 kB       0  [emitted]  main
build.js.map   718 kB       0  [emitted]  main 

我不知道用build.js.map做什么,我不使用它。

但是,Vue 不起作用。虽然我在使用 npm run build 时没有遇到任何错误,但我在控制台中没有看到任何警告,none 我的指令(如 v-bind 有效),我也无法从 [= 访问我的对象 vm 31=]:

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

# adding "export" in front here doesn't help either -
# in browser console it doesn't see `vm` object
const vm = new Vue({
    el: '#app',
    render: h => h(App)
});
在控制台中

vm(或只是 Vue!)。

> vm
VM1256:1 Uncaught ReferenceError: vm is not defined
> Vue
VM1256:1 Uncaught ReferenceError: Vue is not defined

我的 webpack.config.js 看起来像这样:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, '../static/js/vue/'),
    publicPath: '/js/vue/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

npm run build 运行s没有错误,所以我不确定发生了什么。

有什么想法吗?

npm 运行 build 通常还会创建 index.html 文件,或者在您的情况下 'webpack-simple' 它已经存在,其中包含 app.js 或 build.js 就在关闭 body 标签之前。如果您在自己的 html 中包含 build.js,请尝试将其放在 <div id="app"></div> 之后和关闭 </body>.

之前

在底部包含脚本可确保首先加载实际页面内容;最终下载脚本后,内容 (DOM) 将可供您的脚本操作。

此外,如果您:

const vm = new Vue({
    el: '#app',
    render: h => h(App)
});

您无法在控制台中访问 'vm'。在 main.js 中创建的任何变量都不会全局可用。如果您需要它用于调试目的,您可以通过以下方式进行:

window.vm = new Vue({
  el: '#app',
  render: h => h(App)
});

然后您就可以在控制台中访问'vm'。

正如您在 webpack-simple 模板的 index.html 文件中看到的,您应该在 <div id="app"> 元素 之后包含脚本

https://github.com/vuejs-templates/webpack-simple/blob/master/template/index.html#L8-L9

没有全局 Vue 对象是意料之中的事,因为您的捆绑应用不会(也不应该)公开它