【问题标题】:Laravel and Vue JS RouteLaravel 和 Vue JS 路由
【发布时间】:2017-12-16 11:38:24
【问题描述】:

如何在 Vue 组件中使用我的 laravel web.php 的 route('test.create')? 这是web.php:

Route::resource('/test', 'TestController');

但我想通过 route('test.create') 访问 url 并将其添加到我的 Vue 组件中的 <form> 中。 这是 Vue 组件:

<template>
  <div id="app">
    <form action="{{route('test.create')}}" method="POST">
      <div class="form-group">
        <label for="name">Name</label>
        <input type="text" id="name" class="form-control"> 
      </div>
      <div class="form-group">
        <label for="location">Location</label>
        <input type="text" id="location" class="form-control"> 
      </div>
      <div class="form-group">
        <label for="age">Age</label>
        <input type="number" id="age" class="form-control"> 
      </div>
      <div class="form-group">
        <input type="submit" class="btn btn-default"> 
      </div>
    </form>
  </div>
</template>

<script>
export default {
  name: 'create',
  data: function(){
    return {
        msg: '',
    }
  },
}
</script>

【问题讨论】:

标签: php laravel vue.js vue-component vue-router


【解决方案1】:

route('test.create') 是一个 laravel 函数,它不适用于 vue 组件/javascript。您需要通过 DOM(在您的模板/刀片 php 文件中)将返回的值传递给 vue 组件。

例如:

在你的 view.blade.php 中:

<your-component create-url="{{route('test.create')}}"></your-component>

在你的 Component.vue 中:

props: ['createUrl']

那么你可以使用

<form action="{{createUrl}}" method="POST">

【讨论】:

    【解决方案2】:

    你不能访问route()它是一个Laravel helper function它只能在内部工作

    /资源/视图/

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 2018-11-30
      • 2020-12-25
      • 2017-01-12
      • 2017-03-28
      • 2018-02-13
      • 1970-01-01
      • 2021-03-02
      • 2021-02-08
      相关资源
      最近更新 更多