Laravel 如何在blade文件中使用Vue組件 1. 安裝laravel/ui依賴包 composer require laravel/ui 2.生成vue基本腳手架 php artisan ui react 系統還提供了非常便捷的auth腳手架,帶登錄註冊。 php artisan ui r ...
Laravel 如何在blade文件中使用Vue組件
1. 安裝laravel/ui依賴包
composer require laravel/ui
2.生成vue基本腳手架
php artisan ui vue
系統還提供了非常便捷的auth腳手架,帶登錄註冊。
php artisan ui vue --auth
3.組件位置
Vue組件ExampleComponent.vue將被放置在resources/js/components目錄中。ExampleComponent.vue文件是單個文件Vue組件的示例,該組件在同一文件中定義其JavaScript和HTML模板。單個文件組件為構建JavaScript驅動的應用程式提供了一種非常方便的方法。該示例組件已在您的app.js文件中註冊:
Vue.component(
'example-component',
require('./components/ExampleComponent.vue').default
);
4.在blade模版中使用
要在應用程式中使用該組件,您可以將該組件放入Blade模板xxx.blade.php中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>@yield('title')</title>
<link rel="stylesheet" href="{{mix('/css/app.css')}}">
</head>
<body>
<divid="app">
<example-component></example-component>
</divid=>
<script src="{{mix('/js/app.js')}}"></script>
</body>
</html>
註意:在blade文件中一定要有id為app的根節點,而且把組件放到裡面,才能生效!!!