简体   繁体   English

使用 Vue Router 时 Laravel 路由不起作用

[英]Laravel routes not working when using Vue Router

I am trying to call a laravel route Route::get('/logout', 'Auth\\LoginController@logout');我正在尝试调用 Laravel 路由Route::get('/logout', 'Auth\\LoginController@logout'); that will logout the user and redirect to the login page, but when I try to redirect to this url it won't work, anything happens, just like it was calling a vue router route, but this route does not exists in my router.js .这将注销用户并重定向到登录页面,但是当我尝试重定向到这个 url 时它不起作用,任何事情都会发生,就像它在调用 vue 路由器路由一样,但这条路由在我的router.js中不存在router.js

This is my route configuration to vue routes:这是我对 vue 路由的路由配置:

Route::get('/{vue_capture?}', function () {
    return view('index');
})->where('vue_capture', '^(?!storage).*$');

and this is my router.js :这是我的router.js

//import ...

Vue.use(Router)

export default new Router({
    mode: 'history',
    routes: [{
        path: '/',
        component: Inicio
    }, {
        path: '/viagens/cadastrar',
        component: Cadastrar
    }, {
        path: '/viagens/listar',
        component: Listar
    }]
})

There's not a /logout route in my route.js , so why it is not calling my laravel route??我的route.js没有/logout路由,那么为什么它不调用我的 laravel 路由?

How are you generating your links inside your vue component: with router-link or href ?您如何在 vue 组件中生成链接:使用router-linkhref

  • If you want to call a vue route, use router-link ;如果要调用 vue 路由,请使用router-link
  • If you want to call a "normal" or laravel route, use href ;如果要调用“普通”或 Laravel 路由,请使用href

Let me know if it worked.让我知道它是否有效。

尝试将您的 vue 路线放在此行之前:-

Auth::routes();

First, add Laravel auth routes to web.php file in routes folder首先,将 Laravel auth 路由添加到路由文件夹中的 web.php 文件中

Auth::routes();

then in the frontend send a request to logout route然后在前端发送请求注销路由

  axios.post('/logout').then(response => {
                location.reload();
            }).catch((error) => {
               console.log(error);
            });

What I did in myproject.There is Auth::routes() in web.php.我在 myproject 中所做的。web.php 中有 Auth::routes()。 It will handle it by calling ougout in AthenticatesUser.php.Please have a look at this php class.它将通过在 AthenticatesUser.php 中调用 ougout 来处理它。请查看这个 php 类。

<template>
<div class="container">
    <button class="btn" @click="logout">logout</button>
    <form id="logout-form" action="/logout" method="POST" style="display: 
none;">
                        <input type="hidden" name="_token" :value="token">
                    </form>
    <router-view></router-view>
</div>
</template>
 <script>
  export default {
  computed: {
        token() {
            let token = document.head.querySelector('meta[name="csrf-token"]');
            return token.content
        }
    },
    methods: {
        logout() {
            document.getElementById('logout-form').submit()
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM