许多laravel / vue教程使用ajax调用来获取数据。 SPA似乎与Laravel完全隔离。即Laravel只是一个数据API,vue应用程序也可以简单地托管在第三方外部服务器(例如AWS S3)上。这是推荐的方式,还是我应该使用Laravel进行路由并使用单独的视图来实现单个组件和已包含的数据而不是使用SPA?
答案 0 :(得分:0)
对于SPA,我建议仅使用标准设置,即Web服务器上的Laravel和浏览器中的Vue。为此,请安装Laravel和Vue。从浏览器到服务器的AJAX通信是通过Vue附带的Axios库完成的。这是如何安装Laravel和Vue路由器:
composer require laravel/ui
php artisan ui vue
npm install && npm run dev
npm install vue-router
npm run watch
在Vue组件中,使用Axios与服务器通信如下所示。另外,在下面,在Laravel>路由> web.php中定义了端点:
methods: {
fetchMessages() {
let endpoint = `/channels/${this.activeChannel}/messages`;
axios.get(endpoint).then(resp => {
this.messages = resp.data.messages;
});
},
在主js文件中声明了Vue路由器。例如,在app.js中。
这是Vue路由器的外观,其他URL路径将添加到“ routes”下:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
base: '/',
mode: 'history',
history: true,
routes: [
{
path: '/',
name: 'home',
component: PostComponent
},
],
});