newbie to vue.js。在Mac OS上使用版本:
$ npm --version
4.6.1
$ vue --version
2.8.1
我正在将webpack-simple
INIT与VUE-CLI一起用于VUE 2.0。我已经在Django项目文件夹中创建了一个名为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
创建并验证的确实确实由浏览器加载/导入。我将heroku
CLI作为DevServer。
我这样构建:
$ 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
没有任何错误,但我在控制台上没有任何警告,但我的指令均未像v-bind
一样工作,也无法从main.js
访问我的对象vm
:
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)
});
AS 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
毫无疑问地运行,所以我不确定发生了什么。
有什么想法?
npm run构建通常还会创建index.html文件,或者在您的情况下,它已经存在,其中包含app.js或build.js,关闭身体标签。如果您在自己的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
对象的事实,因为您的捆绑包应用程序不会公开它(并且不应该(