是否可以将变量从控制器传递给vuejs?我已经阅读了一些答案,但似乎没有什么对我有用。我想将变量$ url传递给vue。我尝试过这样的事情
var url = {!! $url !!};
这给了我语法错误:app.js中的意外令牌
示例网址http://eventregistry.org/json/suggestConcepts?prefix=sam&lang=eng&callback=JSON_CALLBACK
控制器
class SearchCompanyName extends Controller
{
public function getcompanyname($name)
{
return "http://eventregistry.org/json/suggestConcepts?prefix=" . $name . "&lang=eng&callback=JSON_CALLBACK";
}
public function index()
{
$url = $this->getcompanyname(Input::get("company_name_by_user"));
return view('searchcompany', ['url' => $url]);
}
}
vue app.js
Vue.component('search', require('./components/Searchnames.vue'));
const search = new Vue({
el: '#search',
data: {
},
method: {
getMessages: function(){
console.log('I am here')
}()
}
});
刀片模板
@section('content')
<div class="container">
{!! $url !!}
<search></search>
</div>
@endsection
答案 0 :(得分:1)
如果要在刀片文件中的脚本标记中声明javascript变量,则需要将变量放在引号中,如此
<script>
var url = '{{ $url }}';
</script>
答案 1 :(得分:1)
您可以使用此套件:https://github.com/laracasts/PHP-Vars-To-Js-Transformer
public function index()
{
JavaScript::put([
'foo' => 'bar',
'user' => User::first(),
'age' => 29
]);
return View::make('hello');
}