当前位置: 首页 > news >正文

使用laravel构建spa

为什么80%的码农都做不了架构师?>>>   hot3.png

composer install
npm install
npm add vue-router --save

resources/assets文件夹下新建一个 routes.js ,引入所需的组件,并配置导航:

import Home from './components/Home.vue'
import About from './components/About.vue'

const routes = [
  {
    path: '/',
    component: Home
  },
  {
    path: '/about',
    component: About
  }
]

export default routes

然后在 app.js 里添加路由代码:

import VueRouter from 'vue-router'

Vue.use(VueRouter)

import routes from './routes'
const router = new VueRouter({
  routes
})

/* eslint-disable no-new */
new Vue({
  router,
  el: '#app'
})

 想让laravel支持spa需要配置路由

在 routes/web.php 的结尾处定义一个通用路由:

# Vue
Route::any('{all}', function () {
    return view('app');
})->where(['all' => '.*']);

还有一种组合定义路由的方式,可以传入一个数组参数,数组中包含的 url 都指向同一个 action。就像这样:

Route::combine([
    '/',
    'about'
], function () {
    return view('app');
});

在翻看 Illuminate\Routing\Router 源代码的时候,发现这货其实是可以使用宏的。唔,那就好办了。在 App\Providers\RouteServiceProvider 的 boot 方法添加宏定义:

public function boot()
{
        Route::macro('combine', function(array $uris, $action)
        {
                foreach ($uris as $uri) {
                        Route::any($uri, $action);
                }
        });
        parent::boot();
}

 

 

转载于:https://my.oschina.net/cqqqqq/blog/1611736

相关文章:

  • MyBatis 缓存机制深度解剖 / 自定义二级缓存
  • weex打包android apk采坑之旅(windows)
  • 7-设计模式-代理模式
  • 如何统计序列中元素的出现频度
  • DirectX3D设备丢失(lost device)的处理(一)
  • Ubuntu12.04_X64 apt-get install 报错404
  • 面试必问的volatile,你了解多少?
  • Day04——Python模块
  • [UWP]附加属性2:实现一个Canvas
  • 微信支付demo 报错解决方案:ld: symbol(s) not found for architecture arm64 clang: error: lin
  • java ftp上传方法
  • SQLServer 复制中移除和添加发布而不初始化所有项目
  • gitignore的配置
  • CXF WebService中传递复杂对象(List、Map、Array)
  • 常规活动页面制作
  • hexo+github搭建个人博客
  • [译]CSS 居中(Center)方法大合集
  • Apache Pulsar 2.1 重磅发布
  • cookie和session
  • Java深入 - 深入理解Java集合
  • PermissionScope Swift4 兼容问题
  • Python - 闭包Closure
  • QQ浏览器x5内核的兼容性问题
  • spring boot 整合mybatis 无法输出sql的问题
  • spring-boot List转Page
  • webgl (原生)基础入门指南【一】
  • windows下使用nginx调试简介
  • 大型网站性能监测、分析与优化常见问题QA
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 盘点那些不知名却常用的 Git 操作
  • 前端之Sass/Scss实战笔记
  • 数据科学 第 3 章 11 字符串处理
  • 为什么要用IPython/Jupyter?
  • 项目管理碎碎念系列之一:干系人管理
  • 一个项目push到多个远程Git仓库
  • 用jquery写贪吃蛇
  • #define,static,const,三种常量的区别
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (WSI分类)WSI分类文献小综述 2024
  • (附源码)ssm学生管理系统 毕业设计 141543
  • (过滤器)Filter和(监听器)listener
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (三分钟)速览传统边缘检测算子
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (转)可以带来幸福的一本书
  • .net通用权限框架B/S (三)--MODEL层(2)
  • .NET中的十进制浮点类型,徐汇区网站设计
  • @angular/cli项目构建--http(2)
  • []sim300 GPRS数据收发程序
  • [BZOJ4010]菜肴制作
  • [Django 0-1] Core.Checks 模块
  • [MZ test.16]P2 math 乘方e
  • [Remoting FAQ]Loading a Remoting Host On IIS得到BadImageFormatException
  • [Unity] Unity3D研究院编辑器之独立Inspector属性
  • [Windows][Linux]字体相关