首页 > 解决方案 > 如何在laravel中使用hashtag href创建路由

问题描述

我有指向具有 css 动画的不同页面的菜单链接。我的锚标签看起来像这样

我为此链接创建了一条路线,但它不起作用。

<a href="#about" class="icon-a fas fa-user-tie"></a>

"Route::get('#about',function () {
    return view('about');
});

谁能帮我完成这个。

谢谢

标签: laravel

解决方案


因为片段是由客户端处理的,所以在访问片段之前,客户端必须能够渲染 JavaScript。实现此目的的解决方案可能是:

在您的routes.php文件中:

Route::get('/', function() {
    return view('basic-javascript-router');
});

Route::get('/about', function() {
    return view('about');
});

Route::get('/welcome', function() {
    return view('welcome');
});

在您的项目/resources/views/目录中添加一个简单的basic-javascript-router.php文件:

<!DOCTYPE html>
<html>
<head>
    <title>rerouting</title>
    <script type="text/javascript">
        function checkHash() {
            if (window.location.hash && window.location.hash === '#about') {
                window.location.href = '/about';
            } else {
                //direct users to the generic landing page
                window.location.href = '/welcome';
            }
        }
        window.onload = checkHash;
    </script>
</head>
<body>
    <p>rerouting...</p>
</body>
</html>

通常这是像 VueJS 这样的前端框架的工作,但这可能对你有用。


推荐阅读