TP8笔记250318(本节与视频不符,❤很重要!)


6.8 导航栏区分用户是否登录 6.8.1 在helpers.php中自定义函数: //是否登录 if(!function_exists( isLogin )){ function isLogin(){ return session( ?user ); } } //返回用户实例 if(!function_exists( loginUser )){ funct...

6.8 导航栏区分用户是否登录

6.8.1 在helpers.php中自定义函数:

//是否登录
if(!function_exists('isLogin')){
    function isLogin(){
        return session('?user');
    }
}

//返回用户实例
if(!function_exists('loginUser')){
    function loginUser(){
        return session('user');
    }
}

6.8.2 在_header.html中修改:

<nav class="navbar navbar-expand-lg bg-body-tertiary shadow-sm">
    <div class="container">
        <a class="navbar-brand" href="{:url('home')}">My Weibo</a>
        <ul class="navbar-nav">
            <?php if(isLogin()):?>
            <?php $user = loginUser();?>
            <li class="nav-item">
                <a class="nav-link" aria-current="page" href="{:url('help')}">用户列表</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                    {$user->name}
                </a>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item text-center" href="{:url('User/read',['id'=>$user->id])}">个人中心</a></li>
                    <li><a class="dropdown-item text-center" href="{:url('User/edit',['id'=>$user->id])}">修改个资</a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item text-center" href="#">退出登录</a></li>
                </ul>
            </li>
            <?php else:?>
            <li class="nav-item">
                <a class="nav-link" aria-current="page" href="{:url('help')}">帮助</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="{:url('login.create')}">登录</a>
            </li>
            <?php endif;?>
        </ul>
    </div>
</nav>

6.8.3 但是下拉菜单不起作用,
bootstrap.js:6 Uncaught TypeError: i.createPopper is not a function等,所以,在使用 Bootstrap 5PopoverTooltip 组件时,遇到 bootstrap.js:6 Uncaught TypeError: i.createPopper is not a function错误通常是因为缺少了 @popperjs/core 库的引用。Bootstrap 5依赖于 Popper.js 来正确地定位和显示 TooltipPopover 组件。
原因:“createPopper”不是我们使用引导程序时发生的函数错误 需要popper.js脚本的组件,但我们没有将其加载到页面上 或在引导脚本之后加载它。

要解决此错误,请包括 在运行 JavaScript 代码之前引导捆绑脚本。所以需要引入(UMD):

<script src="https://cdn.bootcdn.net/ajax/libs/popper.js/2.9.3/umd/popper.min.js"></script>

default.html全文如下:

<!doctype html>
<html lang="{:config('app.html_lang')}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    {load href="/css/bootstrap.css" /}
    <link rel="stylesheet" href="/css/base.css?v=<?php echo md5(time()) ; ?>">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <title>{block name="title"}My Weibo{/block}--ThinkPHP8入门实战项目</title>
</head>

<body>
{include file="layouts/_header" /}
<div class="container">
    {include file="shared/_messages" /}
    {block name="content"}{/block}
    {include file="layouts/_footer" /}

</div>
</body>
{js href="/js/jquery.js" /}
<script src="https://cdn.bootcdn.net/ajax/libs/popper.js/2.9.3/umd/popper.min.js"></script>
{js href="/js/bootstrap.js" /}
</html>

该条参考:https://blog.csdn.net/allway2/article/details/128082252


推荐阅读:

收藏

扫描二维码,在手机上阅读

后甩腿对腰的恢复效果好,推荐!

TP8笔记250317

评 论
请登录后再评论
avatar
强哥管理员
  • Chrome
  • Android
本次百度的AI起了大作用,同时F12 排错也派上了用场。
1 个月前