Laravel 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39137883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel is not defined
提问by NEOJPK
Well i am playing with the new laravel 5.3 and vue.js and i want to do a GET call to some users i have in my DB
好吧,我正在使用新的 laravel 5.3 和 vue.js,我想对我数据库中的一些用户进行 GET 调用
Im using components btw.
顺便说一句,我正在使用组件。
This is my app.js
这是我的 app.js
require('./bootstrap');
Vue.component('example', require('./components/UserComponents/User.vue'));
const app = new Vue({
el: 'body',
});
This is my component User.vue i left the HTML template out for post size reason i can post it if neccesary
这是我的组件 User.vue 我出于帖子大小的原因将 HTML 模板放在了外面,如果需要,我可以发布它
<script>
export default{
data : function () {
return {
users : ''
}
},
methods: {
fetchUser: function () {
var vm = this;
vm.$http.get('user/', function (data) {
vm.$set('users', data)
})
}
},
ready() {
this.fetchUser();
},
}
</script>
Im getting 2 errors in the console
我在控制台中收到 2 个错误
vue-resource.common.js?d39b:27 0ReferenceError: Laravel is not defined(…)
(program):29 Uncaught (in promise) ReferenceError: Laravel is not defined(…)
this is my package.json as you can see i have all the deps for this to work vue and vue resource
这是我的 package.json 正如你所看到的,我有所有的 deps 来运行 vue 和 vue 资源
{
"private": true,
"scripts": {
"prod": "gulp --production",
"dev": "gulp watch"
},
"devDependencies": {
"bootstrap-sass": "^3.3.7",
"gulp": "^3.9.1",
"jquery": "^3.1.0",
"laravel-elixir": "^6.0.0-9",
"laravel-elixir-vue": "^0.1.4",
"laravel-elixir-webpack-official": "^1.0.2",
"lodash": "^4.14.0",
"vue": "^1.0.26",
"vue-resource": "^0.9.3"
}
}
Hope someone could help me out here. Thank you
希望有人可以帮助我。谢谢
回答by Mihai Ocneanu
Or for a cleaner format:
或者更简洁的格式:
<script>
window.Laravel = { csrfToken: '{{ csrf_token() }}' };
</script>
Does same thing as Rocco's answer.
与 Rocco 的回答相同。
回答by Rocco Milluzzo
Try to put this on your blade, as you can see is inserted by default on app.blade on Laravel 5.3
尝试将其放在您的刀片上,正如您所看到的,默认情况下已插入 Laravel 5.3 的 app.blade 中
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
回答by omarjebari
if you're using axios then ensure the following is in your bootstrap.js file after axios is imported:
如果您使用的是 axios,请确保在导入 axios 后您的 bootstrap.js 文件中包含以下内容:
window.axios.defaults.headers.common = {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
'X-Requested-With': 'XMLHttpRequest'
};