【发布时间】:2017-09-26 03:06:28
【问题描述】:
这里是 Vue.js 的新手。在 Mac OS 上使用版本:
$ npm --version
4.6.1
$ vue --version
2.8.1
我将 webpack-simple init 与 vue-cli 一起用于 vue 2.0。我在我的 Django 项目文件夹中创建了一个名为 frontend 的 vue 文件夹。目录结构:
$ 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 作为开发服务器运行。
我是这样构建的:
$ 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)
});
在控制台中作为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 运行没有错误,所以我不确定发生了什么。
有什么想法吗?
【问题讨论】:
-
解决了吗?
标签: javascript django vue.js vuejs2 vue-loader