最近升级为Vue-2,使用Broswerify和Vueify。在主应用程序中,我不得不要求vue/dist/vue.js而不是'vue‘,因为我不使用Webpack,但是现在当我使用Vue.use(require('vue-resource'));时,我得到的$http还没有定义。Vue1这件事运行得很顺利。我错过了什么和Vue-2一起工作?
错误:
Uncaught TypeError: Cannot read property 'get' of undefined(…)
Main.js:
require('./bootstrap');
var Vue = require('vue/dist/vue.js');
Vue.use(require('vue-resource'));
// var amazon = require('amazon-affiliate-api');
/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
new Vue({
el: '#wrapper',
mounted: function () {
//use mounted instead of ready.
this.getHttp('/test', this.successFetch);
},
data: {
},
methods: {
successFetch: function (results) {
console.log(results);
},
//vue resource methods
getHttp: function (url,callback) {
const params = {
headers: {
'X-CSRF-TOKEN': this.token
}
}
this.$http.get(url, params).then(callback).catch(err => console.error(err));
},Gulp.js:
const elixir = require('laravel-elixir');
require('laravel-elixir-vue-2');
require('browserify');
require('vueify');
elixir(function(mix){
mix.sass('main.scss')
.browserify('app.js');
});Package.json:
{
"private": true,
"scripts": {
"prod": "gulp --production",
"dev": "gulp watch"
},
"devDependencies": {
"aliasify": "^2.1.0",
"bootstrap-sass": "^3.3.7",
"gulp": "^3.9.1",
"jquery": "^3.1.0",
"laravel-elixir": "^6.0.0-14",
"laravel-elixir-vue-2": "^0.2.0",
"laravel-elixir-webpack-official": "^1.0.2",
"lodash": "^4.16.2",
"vue": "^2.0.1",
"vue-resource": "^1.0.3",
"vueify": "^9.3.0"
},
"dependencies": {
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"browserify": "^13.1.1"
}
}发布于 2016-11-18 07:55:54
如果您只是希望将此设置为SPA (不一定使用laravel),那么我建议您在使用脚手架项目时使用武克利。
在cli中,您会发现一个简单的、更高级的浏览器化设置。
如果您搭建了简单的版本并添加vue-resource:npm install vue-resource -save--dev
然后,您可以按照以下方式调整main.js设置:
import Vue from 'vue'
import VueResource from 'vue-resource'
import App from './App.vue'
Vue.use(VueResource)
new Vue({
el: '#app',
render: h => h(App),
mounted () {
this.getHttp('/')
},
methods: {
getHttp (url) {
this.$http
.get(url)
.then(response => console.log(response))
.catch(err => console.error(err))
},
},
})只是cli构建中的缺省值
如果您正在为laravel构建,那么在Package.json中有许多冗余模块,您可以使用拉拉长生不老药。在npmjs上有一个用于Vue 2.0的叉(从未尝试过):
npm install laravel-elixir-vueify-2.0
我假设您所遇到的问题是由于需要在构建app.js输出时没有被灵丹妙药使用的包。例如,laravel-elixir-vue-2是webpack的建筑--所以很可能什么也不做。
简化您的吞咽设置为:
var elixir = require('laravel-elixir');
require('laravel-elixir-vueify-2.0');
elixir(function(mix) {
mix.sass('main.scss')
.browserify('main.js');
});理论上,您将正确地构建输出并使用Vue 2.0包来构建它。如果将其与上述vue-cli相结合,您应该能够使用经过测试的代码搭建这些早期阶段,从而使故障排除变得更加容易。
发布于 2017-11-15 05:17:15
我将此问题修正为以下模式:
npm install bootstrap-vue --save
npm install vue-resource --save在main.js中
import { Vue, i18n } from './bootstrap'
import BootstrapVue from 'bootstrap-vue'
Vue.use(BootstrapVue)创建bootstrap.js
import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)
export { Vue }在App.vue中
this.$http.get('/api/module/all').then(...https://stackoverflow.com/questions/40667639
复制相似问题